Class: ConstructorPages::Page
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- ConstructorPages::Page
- 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
- .by(params_search = nil) ⇒ Object
-
.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.
- .ids_by_params(params) ⇒ Object
- .in(where_search = nil) ⇒ Object
- .search(what_search = nil) ⇒ Object
- .search_by(params_search = nil) ⇒ Object
- .search_in(where_search = nil) ⇒ Object
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Returns page hash attributes with fields.
- #by(params_search = nil) ⇒ Object
-
#create_fields_values ⇒ Object
Create fields values.
-
#field(code_name) ⇒ Object
Get field by code_name.
-
#find_page_in_branch(cname) ⇒ 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.
-
#multipart? ⇒ Boolean
Return true if there is a file upload field in page.
- #published? ⇒ Boolean
-
#redirect? ⇒ Boolean
Check if link specified.
-
#remove_fields_values ⇒ Object
Remove all fields values.
- #search(what_search = nil) ⇒ Object
- #search_by(params_search = nil) ⇒ Object
-
#set_field_value(code_name, value) ⇒ Object
Set value of field by code_name and value.
-
#touch_branch ⇒ Object
Touch all pages in same branch.
-
#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...
192 193 194 195 196 |
# File 'app/models/constructor_pages/page.rb', line 192 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
66 |
# File 'app/models/constructor_pages/page.rb', line 66 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 |
.ids_by_params(params) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'app/models/constructor_pages/page.rb', line 72 def ids_by_params(params) _hash = {} params.each_pair do |key, value| next if key == 'utf8' key = key.to_s _key, _value = key.gsub(/>|</, ''), value if _value.is_a?(String) next if _value.strip.empty? _value = _value.gsub(/>|</, '') _value = _value.numeric? ? _value.to_f : (_value.to_boolean if _value.boolean?) end sign = '=' if key =~ />$/ || value =~ /^>/ sign = '>' elsif key =~ /<$/ || value =~ /^</ sign = '<' end _fields = ConstructorPages::Field.where(code_name: _key) _ids = [] _fields.each do |_field| _hash[:field_id] = _field.id _ids << _field.type_class.where("value #{sign} #{_value}").where(_hash).map(&:page_id) end _hash[:page_id] = _ids.flatten.uniq end return _hash[:page_id] || [] end |
.in(where_search = nil) ⇒ Object
65 |
# File 'app/models/constructor_pages/page.rb', line 65 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 58 59 60 61 62 63 |
# File 'app/models/constructor_pages/page.rb', line 49 def search(what_search = nil) hash_search, array_search = {}, [] @where_search = Page.find_by(full_url: @where_search) if @where_search.is_a?(String) array_search = ['lft > ? and rgt < ?', @where_search.lft, @where_search.rgt] if @where_search what_search && what_search = what_search.to_s.singularize.downcase hash_search[:template_id] = ConstructorPages::Template.find_by(code_name: what_search).try(:id) if what_search hash_search[:id] = ids_by_params(@params_search) if @params_search @where_search = @params_search = nil hash_search.empty? && array_search.empty? ? [] : Page.where(hash_search).where(array_search).to_a end |
.search_by(params_search = nil) ⇒ Object
69 |
# File 'app/models/constructor_pages/page.rb', line 69 def search_by(params_search = nil); self.by(params_search).search end |
.search_in(where_search = nil) ⇒ Object
68 |
# File 'app/models/constructor_pages/page.rb', line 68 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.
172 173 174 175 176 |
# File 'app/models/constructor_pages/page.rb', line 172 def as_json( = {}) {name: self.name, title: self.title}.merge().tap do || fields.each {|f| .merge!({f.code_name.to_sym => f.get_value_for(self)})} end end |
#by(params_search = nil) ⇒ Object
112 |
# File 'app/models/constructor_pages/page.rb', line 112 def by(params_search = nil); Page.by(params_search); self end |
#create_fields_values ⇒ Object
Create fields values
139 |
# File 'app/models/constructor_pages/page.rb', line 139 def create_fields_values; fields.each {|f| f.create_type_object self} end |
#field(code_name) ⇒ Object
Get field by code_name
116 117 118 |
# File 'app/models/constructor_pages/page.rb', line 116 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
152 153 154 155 156 |
# File 'app/models/constructor_pages/page.rb', line 152 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
121 |
# File 'app/models/constructor_pages/page.rb', line 121 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
163 164 165 166 |
# File 'app/models/constructor_pages/page.rb', line 163 def multipart? fields.each {|f| return true if f.type_value == 'image'} false end |
#published? ⇒ Boolean
160 |
# File 'app/models/constructor_pages/page.rb', line 160 def published?; active? end |
#redirect? ⇒ Boolean
Check if link specified
179 |
# File 'app/models/constructor_pages/page.rb', line 179 def redirect?; url != link && !link.empty? end |
#remove_fields_values ⇒ Object
Remove all fields values
142 |
# File 'app/models/constructor_pages/page.rb', line 142 def remove_fields_values; fields.each {|f| f.remove_type_object self} end |
#search(what_search = nil) ⇒ Object
111 |
# File 'app/models/constructor_pages/page.rb', line 111 def search(what_search = nil); Page.in(self).search(what_search) end |
#search_by(params_search = nil) ⇒ Object
113 |
# File 'app/models/constructor_pages/page.rb', line 113 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
124 |
# File 'app/models/constructor_pages/page.rb', line 124 def set_field_value(code_name, value); field(code_name).try(:set_value_for, self, value) end |
#touch_branch ⇒ Object
Touch all pages in same branch
182 183 184 |
# File 'app/models/constructor_pages/page.rb', line 182 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.
129 130 131 132 133 134 135 136 |
# File 'app/models/constructor_pages/page.rb', line 129 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 |