Class: ConstructorPages::Page

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/constructor_pages/page.rb

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/constructor_pages/page.rb', line 46

def method_missing(name, *args, &block)
  name = name.to_s

  if field(name).nil?
    _template = Template.find_by_code_name(name.singularize)
    template_id = _template.id if _template

    if template_id
      result = []
      result = descendants.where(:template_id => template_id) if name == name.pluralize
      result = ancestors.where(:template_id => template_id).first if result.empty?
      result || []
    end
  else
    field(name)
  end
end

Class Method Details

.children_of(page) ⇒ Object



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

def self.children_of(page)
  Page.where(:parent_id => page)
end

Instance Method Details

#as_json(options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/models/constructor_pages/page.rb', line 64

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

  self.template.fields.each do |field|
    unless self.send(field.code_name)
      options = {field.code_name => self.send(field.code_name)}.merge options
    end
  end

  options
end

#field(code_name, meth = "value") ⇒ Object



37
38
39
40
41
42
43
44
# File 'app/models/constructor_pages/page.rb', line 37

def field(code_name, meth = "value")
  field = ConstructorPages::Field.where(:code_name => code_name, :template_id => self.template_id).first

  if field
    f = "constructor_pages/types/#{field.type_value}_type".classify.constantize.where(:field_id => field.id, :page_id => self.id).first
    f.send(meth) if f
  end
end