Class: ConstructorPages::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ConstructorPages::Page
- Defined in:
- app/models/constructor_pages/page.rb
Overview
Page model. Pages are core for company websites, blogs etc.
Class Method Summary collapse
-
.check_code_name(code_name) ⇒ Object
Check code_name for field and template.
-
.find_by_request_or_first(request = nil) ⇒ Object
Used for find page by request.
-
.full_url_generate(parent_id, url = '') ⇒ Object
Generate full_url from parent id and url.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Returns page hash attributes with fields.
-
#create_fields_values ⇒ Object
Create fields values.
-
#field(code_name) ⇒ Object
Get field by code_name.
-
#find_page_in_branch(code_name) ⇒ Object
(also: #find_pages_in_branch)
Search page by template code_name in same branch of pages and templates.
-
#get_field_value(code_name) ⇒ Object
Get value of field by code_name.
-
#method_missing(name, *args, &block) ⇒ Object
When method missing it get/set field value or get page in branch.
- #published? ⇒ Boolean (also: #active?)
-
#redirect? ⇒ Boolean
Check if link specified.
-
#remove_fields_values ⇒ Object
Remove all fields values.
-
#set_field_value(code_name, value) ⇒ Object
Set value of field by code_name and value.
-
#update_fields_values(params, reset_booleans = true) ⇒ Object
Update all fields values with given params.
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
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
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.
131 132 133 134 135 136 137 138 139 |
# File 'app/models/constructor_pages/page.rb', line 131 def as_json( = {}) = {name: self.name, title: self.title}.merge fields.each do |field| .merge!({field.code_name.to_sym => field.get_value_for(self)}) end end |
#create_fields_values ⇒ Object
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
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?
123 |
# File 'app/models/constructor_pages/page.rb', line 123 def published?; active end |
#redirect? ⇒ Boolean
Check if link specified
142 |
# File 'app/models/constructor_pages/page.rb', line 142 def redirect?; url != link && !link.empty? end |
#remove_fields_values ⇒ Object
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.
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 |