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...


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

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



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

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
# 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 {|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

Parameters:

  • (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:

  • integer

  • (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



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

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

.search(what_search = nil) ⇒ Object



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

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



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

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

.search_in(where_search = nil) ⇒ Object



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

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:

  • (defaults to: {})

    default merge name and title page attributes



125
126
127
128
129
# File 'pages/app/models/constructor_pages/page.rb', line 125

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



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

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

#create_fields_valuesObject

Create fields values



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

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

#field(code_name) ⇒ Object

Get field by code_name



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

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:

  • template code name



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

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



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

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:



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

def published?; active end

#redirect?Boolean

Check if link specified

Returns:



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

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

#remove_fields_valuesObject

Remove all fields values



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

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

#search(what_search = nil) ⇒ Object



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

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

#search_by(params_search = nil) ⇒ Object



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

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
80
81
# File 'pages/app/models/constructor_pages/page.rb', line 79

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:

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

  • (defaults to: true)

    reset all boolean fields to false before assign params



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

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