Class: WebsiteSection

Inherits:
ActiveRecord::Base
  • Object
show all
Extended by:
FriendlyId
Includes:
ErpTechSvcs::Utils::DefaultNestedSetMethods
Defined in:
app/models/website_section.rb

Direct Known Subclasses

Blog, OnlineDocumentSection

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

Class Method Details

.register_type(type) ⇒ Object



35
36
37
38
# File 'app/models/website_section.rb', line 35

def register_type(type)
  @@types << type
  @@types.uniq!
end

Instance Method Details

#articlesObject



54
55
56
# File 'app/models/website_section.rb', line 54

def articles
  Article.find_by_section_id(self.id)
end

#build_section_hashObject



137
138
139
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
166
167
# File 'app/models/website_section.rb', line 137

def build_section_hash
  section_hash = {
    :name => self.title,
    :has_layout => !self.layout.blank?,
    :type => self.class.to_s,
    :in_menu => self.in_menu,
    :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



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

def child_by_path(path)
  self.descendants.detect{|child| child.path == path}
end

#create_layoutObject



95
96
97
98
# File 'app/models/website_section.rb', line 95

def create_layout
  self.layout = IO.read(File.join(WEBSITE_SECTIONS_TEMP_LAYOUT_PATH,"index.html.erb"))
  self.save
end

#get_published_layout(active_publication) ⇒ Object



100
101
102
103
104
105
106
107
108
109
# File 'app/models/website_section.rb', line 100

def get_published_layout(active_publication)
  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
  else
    layout_content = IO.read(File.join(WEBSITE_SECTIONS_TEMP_LAYOUT_PATH,"index.html.erb"))
  end
  layout_content
end

#get_tagsObject



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

def get_tags
  get_topics
end

#get_topicsObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/website_section.rb', line 115

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

#iidObject



50
51
52
# File 'app/models/website_section.rb', line 50

def iid
  internal_identifier
end

#is_document_section?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'app/models/website_section.rb', line 91

def is_document_section?
  type == 'OnlineDocumentSection'
end

#is_section?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'app/models/website_section.rb', line 83

def is_section?
  ['Page', 'Blog'].include? type
end

#is_secured?Boolean

Returns:

  • (Boolean)


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

def is_secured?
  self.protected_with_capability?('view')
end

#pathsObject



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

def paths
  all_paths = [self.path]
  all_paths | self.descendants.collect(&:path)
end

#positioned_childrenObject



66
67
68
# File 'app/models/website_section.rb', line 66

def positioned_children
  children.sort_by{|child| [child.position]}
end

#render_base_layout?Boolean

Returns:

  • (Boolean)


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

def render_base_layout?
  render_base_layout
end

#secureObject



41
42
43
44
45
46
47
48
# File 'app/models/website_section.rb', line 41

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

Returns:

  • (Boolean)


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

def should_generate_new_friendly_id?
  new_record?
end

#typeObject



79
80
81
# File 'app/models/website_section.rb', line 79

def type
  read_attribute(:type) || 'Page'
end

#update_path!Object



131
132
133
134
135
# File 'app/models/website_section.rb', line 131

def update_path!
  new_path = build_path
  self.path = new_path unless self.path == new_path
  self.save
end

#websiteObject



58
59
60
# File 'app/models/website_section.rb', line 58

def website
  website_id.nil? ? self.parent.website : Website.find(website_id)
end