Class: ConstructorPages::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ActiveSupport::Inflector
Defined in:
app/models/constructor_pages/page.rb

Overview

Page model. Pages are core for company websites, blogs etc.

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

When method missing it get/set field value or get page in branch

Examples:

page.content = 'Hello world'
puts page.price
page.brand.models.each do...


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

def method_missing(name, *args, &block)
  super && return if new_record?
  name = name.to_s
  name[-1] == '=' ? set_field_value(name[0..-2], args[0]) : get_field_value(name) || find_pages_in_branch(name)
end

Class Method Details

.by(params_search = nil) ⇒ Object



60
# File 'app/models/constructor_pages/page.rb', line 60

def by(params_search = nil); tap {@params_search = params_search} end

.check_code_name(code_name) ⇒ Object

Check code_name for field and template. When missing method Page find field or page in branch with plural and singular code_name so field and template code_name should be uniqueness for page methods



43
44
45
46
47
# File 'app/models/constructor_pages/page.rb', line 43

def check_code_name(code_name)
  [code_name, code_name.pluralize, code_name.singularize].each {|name|
    return false if Page.instance_methods.include?(name.to_sym)}
  true
end

.find_by_request_or_first(request = nil) ⇒ Object

Used for find page by request. It return first page if no request given or request is home page



29
30
31
# File 'app/models/constructor_pages/page.rb', line 29

def find_by_request_or_first(request = nil)
  (request.nil? || request == '/') ? Page.first : Page.find_by(full_url: request)
end

.full_url_generate(parent_id, url = '') ⇒ Object

Generate full_url from parent id and url



36
37
38
# File 'app/models/constructor_pages/page.rb', line 36

def full_url_generate(parent_id, url = '')
  '/' + Page.find(parent_id).self_and_ancestors.map(&:url).append(url).join('/')
end

.in(where_search = nil) ⇒ Object



59
# File 'app/models/constructor_pages/page.rb', line 59

def in(where_search  = nil); tap {@where_search  = where_search}  end

.search(what_search = nil) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/models/constructor_pages/page.rb', line 49

def search(what_search = nil)
  (@where_search.is_a?(String) ? Page.find_by(full_url: @where_search) : @where_search)
  .tap {|p| @result = @where_search ? p ? p.descendants : [] : Page.all }
  what_search && Template.find_by(code_name: what_search.to_s.singularize.downcase)
  .tap {|t| @result = t ? @result.where(template: t) : [] }
  @params_search && @params_search.each_pair {|k,v| @result = @result.select {|p| p.send(k) == v}}
  @where_search = @params_search = nil
  @result
end

.search_by(params_search = nil) ⇒ Object



63
# File 'app/models/constructor_pages/page.rb', line 63

def search_by(params_search = nil); self.by(params_search).search end

.search_in(where_search = nil) ⇒ Object



62
# File 'app/models/constructor_pages/page.rb', line 62

def search_in(where_search  = nil); self.in(where_search).search  end

Instance Method Details

#as_json(options = {}) ⇒ Object

Returns page hash attributes with fields.

Default attributes are name and title. Options param allows to add more.



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

def as_json(options = {})
  {name: self.name, title: self.title}.merge(options).tap do |options|
    fields.each {|f| options.merge!({f.code_name.to_sym => f.get_value_for(self)})}
  end
end

#by(params_search = nil) ⇒ Object



67
# File 'app/models/constructor_pages/page.rb', line 67

def by(params_search = nil); Page.by(params_search); self end

#create_fields_valuesObject

Create fields values



94
# File 'app/models/constructor_pages/page.rb', line 94

def create_fields_values; fields.each {|f| f.create_type_object self} end

#field(code_name) ⇒ Object

Get field by code_name



71
72
73
# File 'app/models/constructor_pages/page.rb', line 71

def field(code_name)
  Field.find_by code_name: code_name, template_id: template_id
end

#find_page_in_branch(cname) ⇒ Object Also known as: find_pages_in_branch

Search page by template code_name in same branch of pages and templates. It allows to call page.category.brand.series.model etc.

Return one page if founded in ancestors, and return array of pages if founded in descendants

It determines if code_name is singular or nor



107
108
109
110
111
# File 'app/models/constructor_pages/page.rb', line 107

def find_page_in_branch(cname)
  Template.find_by(code_name: cname.singularize).tap {|t| t || return
    (descendants.where(template_id: t.id) if cname == cname.pluralize).tap {|r| r ||= []
      return r.empty? ? ancestors.find_by(template_id: t.id) : r}}
end

#get_field_value(code_name) ⇒ Object

Get value of field by code_name



76
# File 'app/models/constructor_pages/page.rb', line 76

def get_field_value(code_name); field(code_name).try(:get_value_for, self) end

#multipart?Boolean

Return true if there is a file upload field in page



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

def multipart?
  fields.each {|f| return true if f.type_value == 'image'}
  false
end

#published?Boolean



115
# File 'app/models/constructor_pages/page.rb', line 115

def published?; active? end

#redirect?Boolean

Check if link specified



134
# File 'app/models/constructor_pages/page.rb', line 134

def redirect?; url != link && !link.empty? end

#remove_fields_valuesObject

Remove all fields values



97
# File 'app/models/constructor_pages/page.rb', line 97

def remove_fields_values; fields.each {|f| f.remove_type_object self} end

#search(what_search = nil) ⇒ Object



66
# File 'app/models/constructor_pages/page.rb', line 66

def search(what_search = nil); Page.in(self).search(what_search) end

#search_by(params_search = nil) ⇒ Object



68
# File 'app/models/constructor_pages/page.rb', line 68

def search_by(params_search = nil); Page.by(params_search).in(self).search end

#set_field_value(code_name, value) ⇒ Object

Set value of field by code_name and value



79
# File 'app/models/constructor_pages/page.rb', line 79

def set_field_value(code_name, value); field(code_name).try(:set_value_for, self, value) end

#touch_branchObject

Touch all pages in same branch



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

def touch_branch
  [ancestors, descendants].each {|p| p.map(&:touch)}
end

#update_fields_values(params, reset_booleans = true) ⇒ Object

Update all fields values with given params.



84
85
86
87
88
89
90
91
# File 'app/models/constructor_pages/page.rb', line 84

def update_fields_values(params, reset_booleans = true)
  params || return

  fields.each {|f| f.find_type_object(self).tap {|t| t || break
    t.value = 0 if f.type_value == 'boolean' && reset_booleans
    params[f.code_name.to_sym].tap {|v| v && t.value = v}
    t.save }}
end