Class: Qbrick::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
BrickList, Orderable, Searchable, Translatable, RailsSettings::Extend
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



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

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



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

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



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

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

.find_by_path(given_path) ⇒ Object



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

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

.flat_treeObject



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

def flat_tree
  arrange_as_array
end

Instance Method Details

#allowed_brick_typesObject



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

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

#as_jsonObject



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

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

#brick_list_typeObject



205
206
207
# File 'app/models/qbrick/page.rb', line 205

def brick_list_type
  'Qbrick::Page'
end

#cache_keyObject



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

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

#clear_bricks_for_locale(locale) ⇒ Object



225
226
227
228
229
# File 'app/models/qbrick/page.rb', line 225

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



263
264
265
266
267
268
269
270
271
272
273
# File 'app/models/qbrick/page.rb', line 263

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



253
254
255
256
257
258
259
260
261
# File 'app/models/qbrick/page.rb', line 253

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



247
248
249
250
251
# File 'app/models/qbrick/page.rb', line 247

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



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

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



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

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



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

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)


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

def external_redirect?
  redirect? && !internal_redirect?
end

#internal_redirect?Boolean

Returns:

  • (Boolean)


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

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


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

def link
  return children.published.first.link if bricks.empty? && children.any?

  path_with_prefixed_locale
end

Returns:

  • (Boolean)


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

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

#nesting_nameObject



199
200
201
202
203
# File 'app/models/qbrick/page.rb', line 199

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

#parent_pagesObject



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

def parent_pages
  ancestors
end

#path_segmentsObject



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

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

#path_with_prefixed_locale(locale = I18n.locale) ⇒ Object



167
168
169
# File 'app/models/qbrick/page.rb', line 167

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

#published?Boolean

Returns:

  • (Boolean)


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

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

#redirect?Boolean

Returns:

  • (Boolean)


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

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

#remove_preceding_slashesObject



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

def remove_preceding_slashes
  return if redirect_url.blank?

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

#slug_uniquenessObject

class methods



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

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.order(:id).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.sub(/, $/, '')})"
  errors.add :slug, message.html_safe
end

#state_classObject



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

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

#to_style_classObject



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

def to_style_class
  'qbrick-page'
end

#translated?Boolean

Returns:

  • (Boolean)


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

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


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

def translated_link_for(locale)
  return Qbrick::Page.roots.first.link unless translated_to? locale

  I18n.with_locale locale do
    path_with_prefixed_locale
  end
end

#translated_to?(raw_locale) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#update_child_pathsObject



193
194
195
196
197
# File 'app/models/qbrick/page.rb', line 193

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

#urlObject



171
172
173
174
175
# File 'app/models/qbrick/page.rb', line 171

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



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

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