Class: Qbrick::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BrickList, Orderable, Searchable, Translatable
Defined in:
app/models/qbrick/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

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

Methods included from Orderable

included

Class Method Details

.all_pathsObject



60
61
62
63
# File 'app/models/qbrick/page.rb', line 60

def all_paths
  path_columns = column_names.select { |col| col.start_with? 'path_' }
  pluck(*path_columns).flatten.compact.sort.uniq.map(&:path)
end

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



44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/qbrick/page.rb', line 44

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



56
57
58
# File 'app/models/qbrick/page.rb', line 56

def by_identifier(identifier)
  find_by(identifier: identifier)
end

.find_by_path(given_path) ⇒ Object



65
66
67
# File 'app/models/qbrick/page.rb', line 65

def find_by_path(given_path)
  find_by locale_attr(:path) => given_path.blank? ? '' : "/#{given_path.sub(%r{^/+}, '')}"
end

.flat_treeObject



40
41
42
# File 'app/models/qbrick/page.rb', line 40

def flat_tree
  arrange_as_array
end

Instance Method Details

#allowed_brick_typesObject



216
217
218
# File 'app/models/qbrick/page.rb', line 216

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

#as_jsonObject



224
225
226
# File 'app/models/qbrick/page.rb', line 224

def as_json
  { 'title' => title, 'pretty_url' => path, 'url' => "/pages/#{id}" }
end

#brick_list_typeObject



208
209
210
# File 'app/models/qbrick/page.rb', line 208

def brick_list_type
  'Qbrick::Page'
end

#cache_keyObject



220
221
222
# File 'app/models/qbrick/page.rb', line 220

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

#clear_bricks_for_locale(locale) ⇒ Object



228
229
230
231
232
# File 'app/models/qbrick/page.rb', line 228

def clear_bricks_for_locale(locale)
  I18n.with_locale locale do
    bricks.destroy_all
  end
end

#clone_brick_to(brick, to_locale, new_brick_list_id) ⇒ Object



266
267
268
269
270
271
272
273
274
275
276
# File 'app/models/qbrick/page.rb', line 266

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

  copy_assets_to_cloned_brick(brick, new_brick) if brick.uploader?

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

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

  new_brick.save validate: false
end

#clone_bricks_to(locale) ⇒ Object



256
257
258
259
260
261
262
263
264
# File 'app/models/qbrick/page.rb', line 256

def clone_bricks_to(locale)
  failed_to_clone = []
  clear_association_cache

  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



250
251
252
253
254
# File 'app/models/qbrick/page.rb', line 250

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



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'app/models/qbrick/page.rb', line 234

def copy_assets_to_cloned_brick(brick, new_brick)
  uploader_keys = brick.class.uploaders.keys
  multipart_checks = uploader_keys.map { |key| [key, brick.class.uploaders.send(:[], key).ensure_multipart_form] }
  asset_attributes = uploader_keys.map { |key| [key, brick.send(key).path] }

  multipart_checks.each do |uploader_key, _multipart_check|
    brick.class.uploaders.send(:[], uploader_key).ensure_multipart_form = false
  end

  new_brick.update_attributes Hash[asset_attributes]

  multipart_checks.each do |uploader_key, multipart_check|
    brick.class.uploaders.send(:[], uploader_key).ensure_multipart_form = multipart_check
  end
end

#create_pathObject



180
181
182
183
184
185
186
# File 'app/models/qbrick/page.rb', line 180

def create_path
  opts = { locale: I18n.locale }
  path = path_segments.join '/'
  opts[:url] = path if path.present?

  self.path = page_path(opts).sub(%r{^/#{I18n.locale}}, '')
end

#create_slugObject



188
189
190
191
192
193
194
# File 'app/models/qbrick/page.rb', line 188

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

#external_redirect?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/models/qbrick/page.rb', line 119

def external_redirect?
  redirect? && !internal_redirect?
end

#internal_redirect?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
115
116
117
# File 'app/models/qbrick/page.rb', line 106

def internal_redirect?
  return false unless redirect?

  scheme = URI.parse(redirect_url).scheme
  return true if scheme.nil?

  internal_redirect = Qbrick::Engine.hosts.find do |h|
    URI.parse("#{scheme}://#{h}").route_to(redirect_url).host.nil?
  end

  internal_redirect.present?
end


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

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

Returns:

  • (Boolean)


129
130
131
# File 'app/models/qbrick/page.rb', line 129

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

#nesting_nameObject



202
203
204
205
206
# File 'app/models/qbrick/page.rb', line 202

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

#parent_pagesObject



133
134
135
# File 'app/models/qbrick/page.rb', line 133

def parent_pages
  ancestors
end

#path_segmentsObject



164
165
166
167
168
# File 'app/models/qbrick/page.rb', line 164

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

#path_with_prefixed_locale(locale = I18n.locale) ⇒ Object



170
171
172
# File 'app/models/qbrick/page.rb', line 170

def path_with_prefixed_locale(locale = I18n.locale)
  "/#{locale}#{send self.class.attr_name_for_locale(:path, locale)}"
end

#published?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/models/qbrick/page.rb', line 94

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

#redirect?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/models/qbrick/page.rb', line 102

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

#remove_preceding_slashesObject



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

def remove_preceding_slashes
  return if redirect_url.blank?

  redirect_url.sub!(%r{^/+}, '/')
end

#slug_uniquenessObject

class methods



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/models/qbrick/page.rb', line 70

def slug_uniqueness
  path_field = locale_attr :path
  slug_field = locale_attr :slug
  [slug_field, path_field].each do |field|
    self.class.validators_on(field).map { |v| v.validate self }
    return true if errors[field].present?
  end

  page_with_duplicated_paths = self.class.published.translated.where path_field => path
  page_with_duplicated_paths = page_with_duplicated_paths.where.not id: id if persisted?
  return true unless page_with_duplicated_paths.exists?

  message = 'page ids: '
  page_with_duplicated_paths.pluck(:id).each do |id|
    message << "<a href=\"#{edit_cms_page_path id}#page-metadata\" target=\"_blank\">#{id}</a> "
  end
  message = I18n.t 'activerecord.errors.models.qbrick/page.attributes.slug.duplicated_slug', append: " (#{message.strip})"
  errors.add :slug, message.html_safe
end

#state_classObject



98
99
100
# File 'app/models/qbrick/page.rb', line 98

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

#to_style_classObject



212
213
214
# File 'app/models/qbrick/page.rb', line 212

def to_style_class
  'qbrick-page'
end

#translated?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/models/qbrick/page.rb', line 137

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


146
147
148
149
150
151
152
153
154
# File 'app/models/qbrick/page.rb', line 146

def translated_link_for(locale)
  if translated_to? locale
    I18n.with_locale locale do
      path_with_prefixed_locale
    end
  else
    Qbrick::Page.roots.first.link
  end
end

#translated_to?(raw_locale) ⇒ Boolean

Returns:

  • (Boolean)


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

def translated_to?(raw_locale)
  locale = raw_locale.to_s.underscore
  send("title_#{locale}").present? && send("slug_#{locale}").present?
end

#update_child_pathsObject



196
197
198
199
200
# File 'app/models/qbrick/page.rb', line 196

def update_child_paths
  children.each do |child|
    child.update_attribute :path, child.create_path
  end
end

#urlObject



174
175
176
177
178
# File 'app/models/qbrick/page.rb', line 174

def url
  URI::HTTP.build([nil, Qbrick::Engine.host, Qbrick::Engine.port, path_with_prefixed_locale, nil, nil]).tap do |url|
    url.scheme = Qbrick::Engine.scheme
  end
end

#without_selfObject



90
91
92
# File 'app/models/qbrick/page.rb', line 90

def without_self
  self.class.where.not id: id
end