Class: WebsiteSection
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- WebsiteSection
- Extended by:
- FriendlyId
- Includes:
- ErpTechSvcs::Utils::DefaultNestedSetMethods
- Defined in:
- app/models/website_section.rb
Direct Known Subclasses
Constant Summary collapse
- KNIT_KIT_ROOT =
Knitkit::Engine.root.to_s
- WEBSITE_SECTIONS_TEMP_LAYOUT_PATH =
"#{Knitkit::Engine.root.to_s}/app/views/knitkit/website_sections"- @@types =
['Page']
Class Method Summary collapse
Instance Method Summary collapse
- #articles ⇒ Object
- #build_section_hash ⇒ Object
- #child_by_path(path) ⇒ Object
- #children_in_menu ⇒ Object
- #copy(title = nil, copy_articles = false, copy_children = false) ⇒ Object
- #create_layout ⇒ Object
- #display_in_menu? ⇒ Boolean
- #get_published_layout(active_publication) ⇒ Object
- #get_tags ⇒ Object
- #get_topics ⇒ Object
- #iid ⇒ Object
- #is_blog? ⇒ Boolean
- #is_document_section? ⇒ Boolean
- #is_section? ⇒ Boolean
- #is_secured? ⇒ Boolean
- #paths ⇒ Object
- #positioned_children ⇒ Object
- #render_base_layout? ⇒ Boolean
- #secure ⇒ Object
- #should_generate_new_friendly_id? ⇒ Boolean
- #type ⇒ Object
- #update_path! ⇒ Object
- #website ⇒ Object
Class Method Details
.register_type(type) ⇒ Object
36 37 38 39 |
# File 'app/models/website_section.rb', line 36 def register_type(type) @@types << type @@types.uniq! end |
Instance Method Details
#articles ⇒ Object
88 89 90 |
# File 'app/models/website_section.rb', line 88 def articles Article.find_by_section_id(self.id) end |
#build_section_hash ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'app/models/website_section.rb', line 179 def build_section_hash section_hash = { :name => self.title, :has_layout => !self.layout.blank?, :type => self.class.to_s, :in_menu => self., :articles => [], :roles => self.roles.collect(&:internal_identifier), :path => self.path, :permalink => self.permalink, :internal_identifier => self.internal_identifier, :render_base_layout => self.render_base_layout, :position => self.position, :sections => self.children.each.map { |child| child.build_section_hash } } self.contents.each do |content| content_area = content.content_area_by_website_section(self) position = content.position_by_website_section(self) section_hash[:articles] << { :name => content.title, :tag_list => content.tag_list.join(', '), :content_area => content_area, :position => position, :display_title => content.display_title, :internal_identifier => content.internal_identifier } end section_hash end |
#child_by_path(path) ⇒ Object
113 114 115 |
# File 'app/models/website_section.rb', line 113 def child_by_path(path) self.descendants.detect { |child| child.path == path } end |
#children_in_menu ⇒ Object
104 105 106 |
# File 'app/models/website_section.rb', line 104 def positioned_children.where('in_menu = ?', true) end |
#copy(title = nil, copy_articles = false, copy_children = false) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'app/models/website_section.rb', line 42 def copy(title=nil, copy_articles=false, copy_children=false) new_section = WebsiteSection.new new_section.title = title.nil? ? self.title : title new_section.layout = self.layout new_section. = self. new_section.render_base_layout = self.render_base_layout new_section.website_id = self.website_id new_section.save! # copy attached articles if copy_articles self.articles.readonly(false).each do |article| article.website_sections << new_section article.save end end if copy_children self.children.each do |child_section| new_child_section = child_section.copy(nil, copy_articles, copy_children) new_child_section.move_to_child_of(new_section) end end new_section end |
#create_layout ⇒ Object
137 138 139 140 |
# File 'app/models/website_section.rb', line 137 def create_layout self.layout = IO.read(File.join(WEBSITE_SECTIONS_TEMP_LAYOUT_PATH, "index.html.erb")) self.save end |
#display_in_menu? ⇒ Boolean
125 126 127 |
# File 'app/models/website_section.rb', line 125 def self. end |
#get_published_layout(active_publication) ⇒ Object
142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/website_section.rb', line 142 def get_published_layout(active_publication) layout_content = nil published_website_id = active_publication.id published_element = PublishedElement.includes([:published_website]).where('published_websites.id = ? and published_element_record_id = ? and published_element_record_type = ?', published_website_id, self.id, 'WebsiteSection').first unless published_element.nil? layout_content = WebsiteSection::Version.where('version = ? and website_section_id = ?', published_element.version, published_element.published_element_record_id).first.layout end layout_content end |
#get_tags ⇒ Object
153 154 155 |
# File 'app/models/website_section.rb', line 153 def get_topics end |
#get_topics ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'app/models/website_section.rb', line 157 def get_topics # leaving this here for reference until we're sure the built in method tag_counts_on does what we need # sql = "SELECT tags.*, taggings.tags_count AS count FROM \"tags\" # JOIN (SELECT taggings.tag_id, COUNT(taggings.tag_id) AS tags_count FROM \"taggings\" # INNER JOIN contents ON contents.id = taggings.taggable_id AND contents.type = 'Article' # INNER JOIN website_section_contents ON contents.id=website_section_contents.content_id # WHERE (taggings.taggable_type = 'Content' AND taggings.context = 'tags') # AND website_section_contents.website_section_id=#{self.id} # GROUP BY taggings.tag_id HAVING COUNT(*) > 0 AND COUNT(taggings.tag_id) > 0) # AS taggings ON taggings.tag_id = tags.id # ORDER BY tags.name ASC" # ActsAsTaggableOn::Tag.find_by_sql(sql) self.contents.tag_counts_on(:tags).sort_by { |t| t.name } end |
#iid ⇒ Object
84 85 86 |
# File 'app/models/website_section.rb', line 84 def iid internal_identifier end |
#is_blog? ⇒ Boolean
71 72 73 |
# File 'app/models/website_section.rb', line 71 def is_blog? self.is_a?(Blog) || self.attributes['type'] == 'Blog' end |
#is_document_section? ⇒ Boolean
133 134 135 |
# File 'app/models/website_section.rb', line 133 def is_document_section? type == 'OnlineDocumentSection' end |
#is_section? ⇒ Boolean
121 122 123 |
# File 'app/models/website_section.rb', line 121 def is_section? ['Page', 'Blog'].include? type end |
#is_secured? ⇒ Boolean
129 130 131 |
# File 'app/models/website_section.rb', line 129 def is_secured? self.protected_with_capability?('view') end |
#paths ⇒ Object
108 109 110 111 |
# File 'app/models/website_section.rb', line 108 def paths all_paths = [self.path] all_paths | self.descendants.collect(&:path) end |
#positioned_children ⇒ Object
100 101 102 |
# File 'app/models/website_section.rb', line 100 def positioned_children children.order('position') end |
#render_base_layout? ⇒ Boolean
96 97 98 |
# File 'app/models/website_section.rb', line 96 def render_base_layout? render_base_layout end |
#secure ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'app/models/website_section.rb', line 75 def secure capability = self.add_capability(:view) roles = ['admin', 'website_author', self.website.website_role_iid] roles.each do |role| role = SecurityRole.find_by_internal_identifier(role) role.add_capability(capability) end end |
#should_generate_new_friendly_id? ⇒ Boolean
10 11 12 |
# File 'app/models/website_section.rb', line 10 def should_generate_new_friendly_id? new_record? end |
#type ⇒ Object
117 118 119 |
# File 'app/models/website_section.rb', line 117 def type read_attribute(:type) || 'Page' end |
#update_path! ⇒ Object
173 174 175 176 177 |
# File 'app/models/website_section.rb', line 173 def update_path! new_path = build_path self.path = new_path unless self.path == new_path self.save end |
#website ⇒ Object
92 93 94 |
# File 'app/models/website_section.rb', line 92 def website website_id.nil? ? self.parent.website : Website.find(website_id) end |