Class: Content

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/content.rb

Direct Known Subclasses

Article, Link

Defined Under Namespace

Classes: Version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#draftObject

Returns the value of attribute draft.



106
107
108
# File 'app/models/content.rb', line 106

def draft
  @draft
end

Class Method Details

.primaryObject



57
58
59
# File 'app/models/content.rb', line 57

def primary
  published.first
end

Instance Method Details

#author_id=(author_id) ⇒ Object



111
112
113
114
# File 'app/models/content.rb', line 111

def author_id=(author_id)
  # FIXME this is only needed because belongs_to_cacheable can't be non-polymorphic, yet
  self.author = User.find(author_id) if author_id
end

#cache_key(*timestamp_names) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/models/content.rb', line 66

def cache_key(*timestamp_names)
  if new_record?
    "#{model_name.cache_key}/new"
  else
    timestamp_names = [:updated_at, :cells_updated_at] if timestamp_names.none?
    timestamp = timestamp_names.map { |attr| send(attr) }.compact.map(&:to_time).max
    timestamp = timestamp.utc.to_s(cache_timestamp_format)
    "#{model_name.cache_key}/#{id}-#{timestamp}"
  end
end

#category_titlesObject



102
103
104
# File 'app/models/content.rb', line 102

def category_titles
  categories.collect(&:title)
end

#cells_updated_atObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/content.rb', line 77

def cells_updated_at
  if defined?(OutputFilter::Cells)
    OutputFilter::Cells.new(nil).send(:cells, body_html).values.map do |name, state, attrs|
      attrs = HashWithIndifferentAccess.new(attrs)
      cell = "#{name.camelize}Cell".constantize.new
      args = [state]
      attrs.delete "class" # ignore styling class
      attrs[:format] = :timestamp
      args << attrs unless attrs.empty?
      begin
        cell.render_state *args
      rescue ArgumentError
      end
    end.select { |response| response.is_a?(Time) }.max
  end
end

#draft?Boolean

Returns:

  • (Boolean)


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

def draft?
  published_at.nil?
end

#just_published?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/models/content.rb', line 144

def just_published?
  published? && published_at_changed?
end

#ownerObject



98
99
100
# File 'app/models/content.rb', line 98

def owner
  section
end

#ownersObject



94
95
96
# File 'app/models/content.rb', line 94

def owners
  owner.owners << owner
end

#pending?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'app/models/content.rb', line 128

def pending?
  !published?
end

#published?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'app/models/content.rb', line 132

def published?
  !published_at.nil? && published_at <= Time.zone.now
end

#published_at=(published_at) ⇒ Object



107
108
109
# File 'app/models/content.rb', line 107

def published_at=(published_at)
  write_attribute(:published_at, draft.to_i == 1 ? nil : published_at)
end

#published_at?(date) ⇒ Boolean

Returns:

  • (Boolean)


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

def published_at?(date)
  published? && date == [:year, :month, :day].map { |key| published_at.send(key).to_s }
end

#published_monthObject



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

def published_month
  Time.local(published_at.year, published_at.month, 1)
end

#published_yearObject



116
117
118
# File 'app/models/content.rb', line 116

def published_year
  Time.local(published_at.year, 1, 1)
end

#stateObject



140
141
142
# File 'app/models/content.rb', line 140

def state
  pending? ? :pending : :published
end

#to_paramObject



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

def to_param
  permalink
end