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



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

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



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

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



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

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

.find_by_path(given_path) ⇒ Object



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

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

.flat_treeObject



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

def flat_tree
  arrange_as_array
end

Instance Method Details

#allowed_brick_typesObject



189
190
191
# File 'app/models/qbrick/page.rb', line 189

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

#as_jsonObject



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

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

#brick_list_typeObject



181
182
183
# File 'app/models/qbrick/page.rb', line 181

def brick_list_type
  'Qbrick::Page'
end

#cache_keyObject



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

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

#clear_bricks_for_locale(locale) ⇒ Object



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

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



239
240
241
242
243
244
245
246
247
248
249
# File 'app/models/qbrick/page.rb', line 239

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



229
230
231
232
233
234
235
236
237
# File 'app/models/qbrick/page.rb', line 229

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



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

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



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/qbrick/page.rb', line 207

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



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

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



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

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

#external_redirect?Boolean

Returns:

  • (Boolean)


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

def external_redirect?
  redirect? && !internal_redirect?
end

#internal_redirect?Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
# File 'app/models/qbrick/page.rb', line 85

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


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

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

Returns:

  • (Boolean)


108
109
110
# File 'app/models/qbrick/page.rb', line 108

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

#nesting_nameObject



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

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

#parent_pagesObject



112
113
114
# File 'app/models/qbrick/page.rb', line 112

def parent_pages
  ancestors
end

#path_segmentsObject



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

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

#path_with_prefixed_locale(locale = I18n.locale) ⇒ Object



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

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

#published?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/qbrick/page.rb', line 73

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

#redirect?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'app/models/qbrick/page.rb', line 81

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

#remove_preceding_slashesObject



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

def remove_preceding_slashes
  return if redirect_url.blank?

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

#state_classObject



77
78
79
# File 'app/models/qbrick/page.rb', line 77

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

#to_style_classObject



185
186
187
# File 'app/models/qbrick/page.rb', line 185

def to_style_class
  'qbrick-page'
end

#translated?Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/qbrick/page.rb', line 116

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


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

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)


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

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

#update_child_pathsObject



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

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

#without_selfObject

class methods



69
70
71
# File 'app/models/qbrick/page.rb', line 69

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