Class: ConstructorPages::Page

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


150
151
152
153
154
155
156
157
# File 'app/models/constructor_pages/page.rb', line 150

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

Class Method Details

.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
48
49
50
51
52
# File 'app/models/constructor_pages/page.rb', line 43

def self.check_code_name(code_name)
  [code_name, code_name.pluralize, code_name.singularize].each do |name|
    # TODO: replace Page.first
    if Page.first.respond_to?(name)
      return false
    end
  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'



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

def self.find_by_request_or_first(request = nil)
  request.nil? || request == '/' ? Page.first : Page.where(full_url: request).first
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



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

def self.full_url_generate(parent_id, url = '')
  '/' + Page.find(parent_id).self_and_ancestors.map {|c| c.url}.append(url).join('/')
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



131
132
133
134
135
136
137
138
139
# File 'app/models/constructor_pages/page.rb', line 131

def as_json(options = {})
  options = {name: self.name, title: self.title}.merge options

  fields.each do |field|
    options.merge!({field.code_name.to_sym => field.get_value_for(self)})
  end

  options
end

#create_fields_valuesObject

Create fields values



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

def create_fields_values; fields.each {|field| field.create_type_object(self) } end

#field(code_name) ⇒ Object

Get field by code_name



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

def field(code_name)
  Field.where(code_name: code_name, template_id: template_id).first
end

#find_page_in_branch(code_name) ⇒ 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:

  • code_name

    template code name



110
111
112
113
114
115
116
117
118
119
# File 'app/models/constructor_pages/page.rb', line 110

def find_page_in_branch(code_name)
  _template = Template.where(code_name: code_name.singularize).first

  if _template
    result = []
    result = descendants.where(template_id: _template.id) if code_name == code_name.pluralize
    result = ancestors.where(template_id: _template.id).first if result.empty?
    result
  end
end

#get_field_value(code_name) ⇒ Object

Get value of field by code_name



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

def get_field_value(code_name)
  field = field(code_name)
  field.get_value_for(self) if field
end

#published?Boolean Also known as: active?

Returns:

  • (Boolean)


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

def published?; active end

#redirect?Boolean

Check if link specified

Returns:

  • (Boolean)


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

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

#remove_fields_valuesObject

Remove all fields values



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

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

#set_field_value(code_name, value) ⇒ Object

Set value of field by code_name and value



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

def set_field_value(code_name, value)
  field = field(code_name)
  field.set_value_for(self, value) if field
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



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/constructor_pages/page.rb', line 74

def update_fields_values(params, reset_booleans = true)
  return if params.nil?

  fields.each do |field|
    value = params[field.code_name.to_sym]

    _type_object = field.find_type_object(self)

    if _type_object
      _type_object.value = 0 if field.type_value == 'boolean' and reset_booleans

      if value
        _type_object.value = value
      end

      _type_object.save
    end
  end
end