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



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

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

#as_jsonObject



161
162
163
164
165
166
167
# File 'app/models/kuhsaft/page.rb', line 161

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



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

def brick_list_type
  'Kuhsaft::Page'
end

#cache_keyObject



157
158
159
# File 'app/models/kuhsaft/page.rb', line 157

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

#clear_bricks_for_locale(locale) ⇒ Object



169
170
171
# File 'app/models/kuhsaft/page.rb', line 169

def clear_bricks_for_locale(locale)
  bricks.unscoped.where(locale: locale).destroy_all
end

#clone_brick_to(brick, to_locale, new_brick_list_id) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
# File 'app/models/kuhsaft/page.rb', line 194

def clone_brick_to(brick, to_locale, new_brick_list_id)
  new_brick = brick.dup

  copy_assets_to_cloned_brick(brick, new_brick) if brick.uploader?

  new_brick.update_attribute(:locale, to_locale)
  new_brick.update_attribute(:brick_list_id, new_brick_list_id)

  clone_child_bricks(brick, to_locale, new_brick.id) if brick.respond_to?(:bricks)

  new_brick.save
end

#clone_bricks_to(locale) ⇒ Object



185
186
187
188
189
190
191
192
# File 'app/models/kuhsaft/page.rb', line 185

def clone_bricks_to(locale)
  failed_to_clone = []

  bricks.each do |brick|
    failed_to_clone << brick unless clone_brick_to(brick, locale, id)
  end
  failed_to_clone
end

#clone_child_bricks(brick, to_locale, new_brick_list_id) ⇒ Object



179
180
181
182
183
# File 'app/models/kuhsaft/page.rb', line 179

def clone_child_bricks(brick, to_locale, new_brick_list_id)
  brick.bricks.each do |nested_brick|
    clone_brick_to(nested_brick, to_locale, new_brick_list_id)
  end
end

#copy_assets_to_cloned_brick(brick, new_brick) ⇒ Object



173
174
175
176
177
# File 'app/models/kuhsaft/page.rb', line 173

def copy_assets_to_cloned_brick(brick, new_brick)
  brick.class.uploaders.keys.each do |key|
    new_brick.update_attribute(key.to_s, File.open(brick.send(key.to_s).path))
  end
end

#create_slugObject



126
127
128
129
130
131
132
# File 'app/models/kuhsaft/page.rb', line 126

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

#create_urlObject



122
123
124
# File 'app/models/kuhsaft/page.rb', line 122

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


96
97
98
99
100
101
102
# File 'app/models/kuhsaft/page.rb', line 96

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



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

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)



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

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



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

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

#translated_to?(locale) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
# File 'app/models/kuhsaft/page.rb', line 92

def translated_to?(locale)
  send("url_#{locale}").present? && send("title_#{locale}").present? && send("slug_#{locale}").present?
end

#update_child_urlsObject



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

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

#url_with_localeObject



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

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



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

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