Class: Content

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveModel::ForbiddenAttributesProtection, PublicActivity::Common
Defined in:
app/models/content.rb

Direct Known Subclasses

ClientTime, DynamicContent, Graphic, HtmlText, Ticker

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) active



47
48
49
# File 'app/models/content.rb', line 47

def self.active
  where("(start_time IS NULL OR start_time < :now) AND (end_time IS NULL OR end_time > :now)", {:now => Clock.time})
end

+ (Object) all_subclasses

All the subclasses of Content. Conduct a DFS walk of the Content class tree and return the results. This is important because DynamicContent is always 1 step removed from content (Content > DynamicContent > Rss).



150
151
152
153
154
155
156
157
158
159
# File 'app/models/content.rb', line 150

def self.all_subclasses
  sub = []
  sub.concat(self.subclasses)
  self.subclasses.each do |subklass|
    sub.concat(subklass.all_subclasses)
  end
  sub.concat(Concerto::Application.config.content_types)
  sub.concat(Concerto::Application.config._unused_content_types_)
  return sub.uniq { |klass| klass.name }
end

+ (Object) display_name

Display the pretty name of the content type.



162
163
164
165
166
167
168
# File 'app/models/content.rb', line 162

def self.display_name
  if self.const_defined?("DISPLAY_NAME") && !self::DISPLAY_NAME.nil?
    self::DISPLAY_NAME
  else
    self.model_name.human
  end
end

+ (Object) expired

Easily query for active, expired, or future content The scopes are defined as class methods to delay their resolution, defining them as proper scopes will break lots of things, see github.com/concerto/concerto/issues/288.



39
40
41
# File 'app/models/content.rb', line 39

def self.expired
  where("end_time < :now", {:now => Clock.time})
end

+ (Object) filter_all_content(params)



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/models/content.rb', line 200

def self.filter_all_content(params)
  # Check which content, screen, and feed parameters the user has specified
  content_params, subscription_params = {}, {}, {}

  # check user and type parameters
  content_params[:user_id] = params[:user].to_i if params[:user].present?
  content_params[:kind_id] = params[:type].to_i if params[:type].present?
  # check screen and feed parameters
  subscription_params[:screen_id] = params[:screen].to_i if params[:screen].present?
  subscription_params[:feed_id] = params[:feed].to_i if params[:feed].present?

  # Screen parameter is set
  if params[:screen].present?
    # Store all unique subscribed feeds based on requested screen
    #   and store all contents from these feeds
    filtered_contents, screen_feeds = [], []
    # Get screen subscriptions matching search parameters
    Subscription.where(subscription_params).each do |subscription|
      screen_feeds.push(subscription.feed) unless screen_feeds.include?(subscription.feed)
    end
    # Get contents from the screen's feeds
    screen_feeds.each do |feed|
      # Filter out expired and future contents
      if params[:is_active].present?
        filtered_contents += feed.contents.where(content_params).active
      # Allow expired and future contents
      else
        filtered_contents += feed.contents.where(content_params)
      end
    end
    return filtered_contents
  # find contents with feed specified (user and type are optional)
  elsif params[:feed].present?
    filtered_contents = Feed.find(params[:feed].to_i).contents.where(content_params)
  # find contents with user and/or type specified
  else
    filtered_contents = Content.where(content_params)
  end

  # return only active content
  if params[:is_active].present?
    return filtered_contents.active
  # return content regardless of active status
  else
    return filtered_contents
  end
end

+ (Object) form_attributes

Define the attributes that will be allowed via strong_parameters. We define a common set of attribtues here, expecting child content types to supplement this list with additional attributes that they require.



142
143
144
# File 'app/models/content.rb', line 142

def self.form_attributes
  attributes = [:name, :duration, :data, {:start_time => [:time, :date]}, {:end_time => [:time, :date]}]
end

+ (Object) future



43
44
45
# File 'app/models/content.rb', line 43

def self.future
  where("start_time > :now", {:now => Clock.time})
end

+ (Object) preview(*arg)

Allow the subclasses to render a preview of their content



130
131
132
# File 'app/models/content.rb', line 130

def self.preview(*arg)
  ""
end

Instance Method Details

- (Boolean) action_allowed?(action_name, user)

Figure out if a user should be able to run a custom action. Default to any user runs no actions.



172
173
174
# File 'app/models/content.rb', line 172

def action_allowed?(action_name, user)
  return false
end

- (Object) after_add_callback(unused_submission)

A placeholder for any action that should be performed after content has been submitted to a feed.



189
190
# File 'app/models/content.rb', line 189

def after_add_callback(unused_submission)
end

- (Boolean) can_display_in?(screen, field)

Determine if a piece of content should be displayed based on screen and field. By default content can be rendered in any Field which has the same Kind as the Content. This can be overridden by different content types which can be displayed in different fields or based on some dynamic criteria implemented in each Content subclass.



196
197
198
# File 'app/models/content.rb', line 196

def can_display_in?(screen, field)
  return self.kind == field.kind
end

- (Object) cannot_be_own_parent



19
20
21
22
23
# File 'app/models/content.rb', line 19

def cannot_be_own_parent
  if !parent_id.blank? and parent_id == id
    errors.add(:parent_id, I18n.t(:cant_be_this_content))
  end
end

- (Object) end_time=(_end_time)

See start_time=.



107
108
109
110
111
112
113
114
115
116
# File 'app/models/content.rb', line 107

def end_time=(_end_time)
  if _end_time.kind_of?(Hash)
    return if _end_time[:date].empty?
    # convert to time, strip off the timezone offset so it reflects local time
    t = DateTime.strptime("#{_end_time[:date]} #{_end_time[:time]}", "%m/%d/%Y %l:%M %p")
    write_attribute(:end_time, Time.zone.parse(Time.iso8601(t.to_s).to_s(:db)).to_s(:db))
  else
    write_attribute(:end_time, _end_time)
  end
end

- (Boolean) has_children?

A quick test to see if a content has any children



135
136
137
# File 'app/models/content.rb', line 135

def has_children?
  !self.children.empty?
end

- (Boolean) is_active?

Determine if content is active based on its start and end times. Content is active if two conditions are met:

  1. Start date is before now, or nil.

  2. End date is after now, or nil.



63
64
65
# File 'app/models/content.rb', line 63

def is_active?
  (start_time.nil? || start_time < Clock.time) && (end_time.nil? || end_time > Clock.time)
end

- (Boolean) is_approved?

Determine if content is approved everywhere



77
78
79
# File 'app/models/content.rb', line 77

def is_approved?
  (self.approved_feeds.count > 0) && ((self.pending_feeds.count + self.denied_feeds.count) == 0)
end

- (Boolean) is_denied?

Determine if content is denied on a feed



87
88
89
# File 'app/models/content.rb', line 87

def is_denied?
  (self.denied_feeds.count > 0)
end

- (Boolean) is_expired?

Determine if content is expired based on its end time.



68
69
70
# File 'app/models/content.rb', line 68

def is_expired?
  (!end_time.nil? and end_time < Clock.time)
end

- (Boolean) is_orphan?



72
73
74
# File 'app/models/content.rb', line 72

def is_orphan?
  self.submissions.empty?
end

- (Boolean) is_pending?

Determine if content is pending on a feed



82
83
84
# File 'app/models/content.rb', line 82

def is_pending?
  (self.pending_feeds.count > 0)
end

- (Object) perform_action(action_name, options)

Perform custom actions on a piece of content. Accessed via /content/:id/act?action_name=action_name&options. Returns nil if there was a problem, otherwise return the result of the action.



179
180
181
182
183
184
185
# File 'app/models/content.rb', line 179

def perform_action(action_name, options)
  if action_allowed?(action_name, options[:current_user])
    return send(action_name, options)
  else
    return nil
  end
end

- (Object) pre_render(*arg)

A placeholder for a pre-rendering processing trigger.



120
121
122
# File 'app/models/content.rb', line 120

def pre_render(*arg)
  true
end

- (Object) render_details

The additional data required when rendering this content.



125
126
127
# File 'app/models/content.rb', line 125

def render_details
  {:data => self.data}
end

- (Object) start_time=(_start_time)

Setter for the start time. If a hash is passed, convert that into a DateTime object and then a string. Otherwise, just set it like normal. This is a bit confusing due to the differences in how Ruby handles times between 1.9.x and 1.8.x.



94
95
96
97
98
99
100
101
102
103
104
# File 'app/models/content.rb', line 94

def start_time=(_start_time)
  if _start_time.kind_of?(Hash)
    return if _start_time[:date].empty?
    #write_attribute(:start_time, Time.parse("#{_start_time[:date]} #{_start_time[:time]}").to_s(:db))
    # convert to time, strip off the timezone offset so it reflects local time
    t = DateTime.strptime("#{_start_time[:date]} #{_start_time[:time]}", "%m/%d/%Y %l:%M %p")
    write_attribute(:start_time, Time.zone.parse(Time.iso8601(t.to_s).to_s(:db)).to_s(:db))
  else
    write_attribute(:start_time, _start_time)
  end
end