Class: Content

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

Direct Known Subclasses

Article, DocumentedContent

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.do_search(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'app/models/content.rb', line 59

def self.do_search(options = {})
  @results = Content.search(options)

  @search_results = build_search_results(@results)

  @page_results = WillPaginate::Collection.create(options[:page], options[:per_page], @results.total_entries) do |pager|
    pager.replace(@search_results)
  end

  @page_results
end

.find_by_section_id(website_section_id) ⇒ Object



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

def self.find_by_section_id(website_section_id)
  Content.joins(:website_section_contents).where('website_section_id = ?', website_section_id).order("website_section_contents.position ASC, website_section_contents.created_at DESC").all
end

.find_by_section_id_filtered_by_id(website_section_id, id_filter_list) ⇒ Object



75
76
77
# File 'app/models/content.rb', line 75

def self.find_by_section_id_filtered_by_id(website_section_id, id_filter_list)
  Content.joins(:website_section_contents).where("website_section_id = ? AND contents.id IN (#{id_filter_list.join(',')})", website_section_id).all
end

.find_published_by_section(active_publication, website_section) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'app/models/content.rb', line 79

def self.find_published_by_section(active_publication, website_section)
  published_content = []
  contents = self.find_by_section_id(website_section.id)
  contents.each do |content|
    content = get_published_version(active_publication, content)
    published_content << content unless content.nil?
  end

  published_content
end

.find_published_by_section_with_tag(active_publication, website_section, tag) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/content.rb', line 90

def self.find_published_by_section_with_tag(active_publication, website_section, tag)
  published_content = []
  id_filter_list = self.tagged_with(tag.name).collect { |t| t.id }
  contents = self.find_by_section_id_filtered_by_id(website_section.id, id_filter_list)
  contents.each do |content|
    content = get_published_version(active_publication, content)
    published_content << content unless content.nil?
  end

  published_content
end

.search(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/content.rb', line 29

def self.search(options = {})
  predicate = Content.includes([:website_sections])

  unless options[:section_unique_name].blank?
    predicate = predicate.where("website_sections.internal_identifier = ?", options[:section_unique_name])
  end

  unless options[:parent_id].blank?
    predicate = predicate.where("website_sections.id" => WebsiteSection.find(options[:parent_id]).self_and_descendants.collect(&:id))
  end

  unless options[:content_type].blank?
    predicate = predicate.where("website_sections.type = ?", options[:content_type])
  end

  unless options[:website_id].blank?
    predicate = predicate.where("website_sections.website_id = ?", options[:website_id])
  end

  predicate = predicate.where("(UPPER(contents.title) LIKE UPPER('%#{options[:query]}%')
                    OR UPPER(contents.excerpt_html) LIKE UPPER('%#{options[:query]}%') 
                    OR UPPER(contents.body_html) LIKE UPPER('%#{options[:query]}%') )").order("contents.created_at DESC")

  if options[:page]
    predicate.paginate(:page => options[:page], :per_page => options[:per_page])
  else
    predicate.all
  end
end

Instance Method Details

#assign_attribute_on_saveObject



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/models/content.rb', line 140

def assign_attribute_on_save
  super

  Article.find_by_internal_identifier(self.internal_identifier) ? attribute_type_description = "updated_by_role" : attribute_type_description = "created_by_role"

  if attribute_type_description == "created_by_role"
    user = self.created_by
  else
    user = User.find(self.versions.sort_by { |version| version.version }.reverse[0].created_by_id)
  end

  if user
    #keep only the attributes related to the most recent change for each attribute_type
    self.destroy_values_of_type attribute_type_description

    attribute_type = AttributeType.find_by_internal_identifier(attribute_type_description)
    attribute_type = AttributeType.create(:description => attribute_type_description, :data_type => "Text") unless attribute_type

    user.roles.each do |role|
      new_value = AttributeValue.new(:value => role.internal_identifier)
      attribute_type.attribute_values << new_value
      self.attribute_values << new_value
      new_value.save
    end
  end
end

#content_area_by_website_section(section) ⇒ Object



124
125
126
127
128
129
130
# File 'app/models/content.rb', line 124

def content_area_by_website_section(section)
  content_area = nil
  unless WebsiteSectionContent.where('content_id = ? and website_section_id = ?', self.id, section.id).first.nil?
    content_area = WebsiteSectionContent.where('content_id = ? and website_section_id = ?', self.id, section.id).first.content_area
  end
  content_area
end

#find_website_sections_by_site_id(website_id) ⇒ Object



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

def find_website_sections_by_site_id(website_id)
  self.website_sections.where('website_id = ?', website_id).all
end

#get_comments(limit) ⇒ Object



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

def get_comments(limit)
  self.comments.recent.limit(limit).all
end

#is_published?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'app/models/content.rb', line 167

def is_published?
  !PublishedElement.where('published_element_record_id = ? and published_element_record_type = ? and published_elements.version = ?', self.id, 'Content', self.version).first.nil?
end

#position(website_section_id) ⇒ Object



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

def position(website_section_id)
  position = self.website_section_contents.find_by_website_section_id(website_section_id).position
  position
end

#position_by_website_section(section) ⇒ Object



132
133
134
135
136
137
138
# File 'app/models/content.rb', line 132

def position_by_website_section(section)
  position = nil
  unless WebsiteSectionContent.where('content_id = ? and website_section_id = ?', self.id, section.id).first.nil?
    position = WebsiteSectionContent.where('content_id = ? and website_section_id = ?', self.id, section.id).first.position
  end
  position
end

#should_generate_new_friendly_id?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/models/content.rb', line 9

def should_generate_new_friendly_id?
  new_record?
end

#update_content_area_and_position_by_section(section, content_area, position) ⇒ Object



115
116
117
118
119
120
121
122
# File 'app/models/content.rb', line 115

def update_content_area_and_position_by_section(section, content_area, position)
  website_section_content = WebsiteSectionContent.where('content_id = ? and website_section_id = ?', self.id, section.id).first
  unless website_section_content.nil?
    website_section_content.content_area = content_area
    website_section_content.position = position
    website_section_content.save
  end
end