Class: WebsiteSection

Inherits:
ActiveRecord::Base
  • Object
show all
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



27
28
29
30
# File 'app/models/website_section.rb', line 27

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

Instance Method Details

#articlesObject



46
47
48
# File 'app/models/website_section.rb', line 46

def articles
  Article.find_by_section_id(self.id)
end

#build_section_hashObject



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'app/models/website_section.rb', line 130

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 => [],
    :is_secured => self.is_secured?,
    :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



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

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

#create_layoutObject



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

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



92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/website_section.rb', line 92

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

#get_tagsObject



104
105
106
# File 'app/models/website_section.rb', line 104

def get_tags
  get_topics
end

#get_topicsObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/website_section.rb', line 108

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



42
43
44
# File 'app/models/website_section.rb', line 42

def iid
  internal_identifier
end

#is_document_section?Boolean

Returns:

  • (Boolean)


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

def is_document_section?
  type == 'OnlineDocumentSection'
end

#is_section?Boolean

Returns:

  • (Boolean)


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

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

#is_secured?Boolean

Returns:

  • (Boolean)


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

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

#pathsObject



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

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

#positioned_childrenObject



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

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

#render_base_layout?Boolean

Returns:

  • (Boolean)


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

def render_base_layout?
  render_base_layout
end

#secureObject



33
34
35
36
37
38
39
40
# File 'app/models/website_section.rb', line 33

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

#typeObject



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

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

#update_path!Object



124
125
126
127
128
# File 'app/models/website_section.rb', line 124

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

#websiteObject



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

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