Class: ConstructorPages::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
pages/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...


142
143
144
145
146
# File 'pages/app/models/constructor_pages/page.rb', line 142

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 'pages/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



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

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

  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

Parameters:

  • request (defaults to: nil) —

    for example '/conditioners/split-systems/zanussi'



27
28
29
# File 'pages/app/models/constructor_pages/page.rb', line 27

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

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

Generate full_url from parent id and url

Parameters:

  • parent_id —

    integer

  • url (defaults to: '') —

    should looks like 'hello-world-page' without leading slash



34
35
36
# File 'pages/app/models/constructor_pages/page.rb', line 34

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 'pages/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 'pages/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 'pages/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 'pages/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.

Parameters:

  • options (defaults to: {}) —

    default merge name and title page attributes



127
128
129
130
131
# File 'pages/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 'pages/app/models/constructor_pages/page.rb', line 67

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

#create_fields_values ⇒ Object

Create fields values



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

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 'pages/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

Parameters:

  • cname —

    template code name



111
112
113
114
115
# File 'pages/app/models/constructor_pages/page.rb', line 111

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
77
78
# File 'pages/app/models/constructor_pages/page.rb', line 76

def get_field_value(code_name)
  field(code_name).tap {|f| return f.get_value_for(self) if f}
end

#published? ⇒ Boolean Also known as: active?

Returns:

  • (Boolean)


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

def published?; active end

#redirect? ⇒ Boolean

Check if link specified

Returns:

  • (Boolean)


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

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

#remove_fields_values ⇒ Object

Remove all fields values



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

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

#search(what_search = nil) ⇒ Object



66
# File 'pages/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 'pages/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



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

def set_field_value(code_name, value)
  field(code_name).tap {|f| f.set_value_for(self, value) if f}
end

#update_fields_values(params, reset_booleans = true) ⇒ Object

Update all fields values with given params.

Parameters:

  • params —

    should looks like {price: 500, content: 'Hello'}

  • reset_booleans (defaults to: true) —

    reset all boolean fields to false before assign params



88
89
90
91
92
93
94
95
# File 'pages/app/models/constructor_pages/page.rb', line 88

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