Class: Kuhsaft::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BrickList, Orderable, Searchable, Translatable
Defined in:
app/models/kuhsaft/page.rb

Constant Summary

Constants included from Searchable

Searchable::DICTIONARIES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Searchable

#update_fulltext

Methods included from BrickList

#brick_types, #collect_fulltext, included, #renders_own_childs?, #to_brick_item_id, #to_brick_list_id, #uploader?, #user_can_add_childs?, #user_can_delete?, #user_can_save?

Methods included from Translatable

included

Methods included from Translatable::ClassMethods

#define_localized_attr_finder, #define_localized_attr_getter, #define_localized_attr_predicate_method, #define_localized_attr_setter, #locale_attr, #locale_for_attr_name, #translate

Methods included from Orderable

included

Class Method Details

.all_urlsObject



58
59
60
61
# File 'app/models/kuhsaft/page.rb', line 58

def all_urls
  url_columns = column_names.select { |col| col.start_with? 'url_' }
  pluck(url_columns).flatten.compact.sort.uniq.map { |r| "/#{r}" }
end

.arrange_as_array(options = {}, hash = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'app/models/kuhsaft/page.rb', line 42

def arrange_as_array(options = {}, hash = nil)
  hash ||= arrange(options)

  arr = []
  hash.each do |node, children|
    arr << node
    arr += arrange_as_array(options, children) unless children.empty?
  end

  arr
end

.by_identifier(identifier) ⇒ Object



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

def by_identifier(identifier)
  where(identifier: identifier).first
end

.flat_treeObject



38
39
40
# File 'app/models/kuhsaft/page.rb', line 38

def flat_tree
  arrange_as_array
end

Instance Method Details

#allowed_brick_typesObject



149
150
151
# File 'app/models/kuhsaft/page.rb', line 149

def allowed_brick_types
  Kuhsaft::BrickType.enabled.pluck(:class_name) - ['Kuhsaft::AccordionItemBrick']
end

#as_jsonObject



157
158
159
160
161
162
163
# File 'app/models/kuhsaft/page.rb', line 157

def as_json
  Hash.new.tap do |json|
    json['title'] = send("title_#{I18n.locale.to_s.underscore}")
    json['pretty_url'] = '/' + send("url_#{I18n.locale.to_s.underscore}")
    json['url'] = "/pages/#{id}"
  end
end

#brick_list_typeObject



141
142
143
# File 'app/models/kuhsaft/page.rb', line 141

def brick_list_type
  'Kuhsaft::Page'
end

#cache_keyObject



153
154
155
# File 'app/models/kuhsaft/page.rb', line 153

def cache_key
  super + bricks.map(&:cache_key).join
end

#create_slugObject



122
123
124
125
126
127
128
# File 'app/models/kuhsaft/page.rb', line 122

def create_slug
  if title.present? && slug.blank?
    self.slug = title.downcase.parameterize
  elsif slug.present?
    self.slug = slug.downcase
  end
end

#create_urlObject



118
119
120
# File 'app/models/kuhsaft/page.rb', line 118

def create_url
  self.url = url_with_locale[1..-1]
end


92
93
94
95
96
97
98
# File 'app/models/kuhsaft/page.rb', line 92

def link
  if bricks.count == 0 && children.count > 0
    children.first.link
  else
    url_with_locale
  end
end

Returns:

  • (Boolean)


80
81
82
# File 'app/models/kuhsaft/page.rb', line 80

def navigation?
  page_type == Kuhsaft::PageType::NAVIGATION
end

#nesting_nameObject



135
136
137
138
139
# File 'app/models/kuhsaft/page.rb', line 135

def nesting_name
  num_dashes = parent_pages.size
  num_dashes = 0 if num_dashes < 0
  "#{'-' * num_dashes} #{title}".strip
end

#parent_pagesObject



84
85
86
# File 'app/models/kuhsaft/page.rb', line 84

def parent_pages
  ancestors
end

#path_segmentsObject

TODO: needs naming and routing refactoring (url/locale/path/slug)



101
102
103
104
105
# File 'app/models/kuhsaft/page.rb', line 101

def path_segments
  paths = parent.present? ? parent.path_segments : []
  paths << slug unless navigation?
  paths
end

#published?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/models/kuhsaft/page.rb', line 68

def published?
  published == Kuhsaft::PublishState::PUBLISHED
end

#redirect?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'app/models/kuhsaft/page.rb', line 76

def redirect?
  page_type == Kuhsaft::PageType::REDIRECT || page_type == Kuhsaft::PageType::CUSTOM
end

#state_classObject



72
73
74
# File 'app/models/kuhsaft/page.rb', line 72

def state_class
  published? ? 'published' : 'unpublished'
end

#to_style_classObject



145
146
147
# File 'app/models/kuhsaft/page.rb', line 145

def to_style_class
  'kuhsaft-page'
end

#translated?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'app/models/kuhsaft/page.rb', line 88

def translated?
  url.present? && title.present? && slug.present?
end

#update_child_urlsObject



130
131
132
133
# File 'app/models/kuhsaft/page.rb', line 130

def update_child_urls
  return unless children.any?
  children.each { |child| child.update_attributes(url: child.create_url) }
end

#url_with_localeObject



111
112
113
114
115
116
# File 'app/models/kuhsaft/page.rb', line 111

def url_with_locale
  opts = { locale: I18n.locale }
  url = url_without_locale
  opts[:url] = url if url.present?
  page_path(opts)
end

#url_without_localeObject



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

def url_without_locale
  path_segments.join('/')
end

#without_selfObject



64
65
66
# File 'app/models/kuhsaft/page.rb', line 64

def without_self
  self.class.where 'id != ?', id
end