class EventNewsSetting include Mongoid::Document include Mongoid::Timestamps field :carousel_image_width, type: String, :default => "75%" field :top_limit, type: Integer, :default => 0 field :pro_enabled, type: Boolean, :default => false field :approvers, type: Array, :default => [] field :email_to, type: Array, :default => ["admins","managers","approvers"] field :is_display_edit_only, type: Boolean, :default => false field :only_manager_can_edit_status, type: Boolean, :default => false field :including_day_of_the_week, type: Boolean, default: true field :including_time, type: Boolean, default: true field :hour_clock_24, type: Boolean, default: true has_many :event_news_status_settings, :autosave => true, :dependent => :destroy accepts_nested_attributes_for :event_news_status_settings, :allow_destroy => true after_save do if self.including_day_of_the_week_changed? || self.including_time_changed? || self.hour_clock_24_changed? EventNewsCache.all.destroy end end def self.check_limit_for_user(user_id, b_id = nil) limit = self.first.top_limit rescue 0 return true if limit == 0 count = Bulletin.where(:is_top => true, :create_user_id => user_id, :id.ne => b_id).count return count < limit end def self.is_pro? self.first.pro_enabled rescue false end def self.event_date_default_format setting = self.first setting.event_date_default_format end def event_date_default_format datetime_format = "%Y-%m-%d" if self.including_day_of_the_week datetime_format += " %a" end if self.including_time if self.hour_clock_24 datetime_format += " %H:%M" else datetime_format += " %I:%M %P" end end datetime_format end def event_date_format @event_date_format ||= self.event_date_default_format end def self.event_time_formated_for_frontend(st,ed) self.first.event_time_formated_for_frontend(st,ed) end def event_time_formated_for_frontend(st,ed) st,ed = self.event_time_formated(st,ed).split("~") if st.blank? st = "#{st}" end if ed.nil? st else ed = "#{ed}" "#{st} ~ #{ed}" end end def event_time_formated(st,ed) s = st e = ed if s.blank? && e.blank? "" elsif e.blank? custom_strftime(s, self.event_date_format) elsif s.blank? "~ " + custom_strftime(e, self.event_date_format) else parse_format = self.event_date_format if s.to_date == e.to_date parse_format_arr = parse_format.split(/(%d %a|%d) /) parse_format_arr = parse_format_arr[0..-2].join(""), parse_format_arr[-1] date_str = custom_strftime(s, parse_format_arr[0]) s_time = s.strftime(parse_format_arr[1]) e_time = e.strftime(parse_format_arr[1]) "#{date_str} #{s_time} ~ #{e_time}" else custom_strftime(s, self.event_date_format) + " ~ " + custom_strftime(e, self.event_date_format) end end end end