Class: Modular::Components::Base

Inherits:
AbstractController::Base
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
AbstractController::Helpers, AbstractController::Rendering, ActiveModel::Conversion, ActiveModel::Validations, Rendering
Defined in:
lib/modular/components/base.rb

Direct Known Subclasses

Container, MainContent

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rendering

#action_name, #execute, #render

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/modular/components/base.rb', line 69

def initialize(attributes = {})
  append_view_path(Rails.root.join('app', 'views'))
  append_view_path(Gem.loaded_specs['modular'].full_gem_path + '/templates')

  @attributes ||= ActiveSupport::HashWithIndifferentAccess.new
  
  @attributes[:uniqid] = ''
  @attributes[:width] = 0

  attributes.each do |name, value|
    @attributes[name.to_s] = value
  end
end

Instance Attribute Details

#titleObject

params for element itself



63
64
65
# File 'lib/modular/components/base.rb', line 63

def title
  @title
end

#uniqidObject

Returns the value of attribute uniqid.



64
65
66
# File 'lib/modular/components/base.rb', line 64

def uniqid
  @uniqid
end

#widthObject

Returns the value of attribute width.



65
66
67
# File 'lib/modular/components/base.rb', line 65

def width
  @width
end

Class Method Details

.attr_accessor(*fields) ⇒ Object



55
56
57
58
59
60
# File 'lib/modular/components/base.rb', line 55

def self.attr_accessor(*fields)
  fields.each do |field|
    attr_writer field
    attr_reader field
  end
end

.attr_reader(*fields) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/modular/components/base.rb', line 35

def self.attr_reader(*fields)
  fields.each do |field|
    class_eval <<EOF
  def #{field}
    @attributes['#{field}']
  end
EOF
  end
end

.attr_writer(*fields) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/modular/components/base.rb', line 45

def self.attr_writer(*fields)
  fields.each do |field|
    class_eval <<EOF
  def #{field}=(value)
    @attributes['#{field}']=value
  end
EOF
  end
end

.typeObject

slug



16
17
18
# File 'lib/modular/components/base.rb', line 16

def self.type
  self.name.demodulize.underscore
end

.use_mustached_template!Object



24
25
26
27
28
29
# File 'lib/modular/components/base.rb', line 24

def self.use_mustached_template!
  require 'mustache'
  self.class_eval do
    include Modular::MstRendering
  end
end

Instance Method Details

#all_errorsObject



95
96
97
98
# File 'lib/modular/components/base.rb', line 95

def all_errors
  valid? #this call is used to make validation calls
  { uniqid => errors.to_a }
end

#find_by_uniqid(id) ⇒ Object



100
101
102
# File 'lib/modular/components/base.rb', line 100

def find_by_uniqid(id)
  self if id.to_s == uniqid.to_s
end

#persisted?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/modular/components/base.rb', line 83

def persisted?
  false
end

#to_hashObject



87
88
89
# File 'lib/modular/components/base.rb', line 87

def to_hash
  @attributes.merge({:type => type})
end

#to_jsonObject



91
92
93
# File 'lib/modular/components/base.rb', line 91

def to_json
  ActiveSupport::JSON.encode to_hash
end

#typeObject



20
21
22
# File 'lib/modular/components/base.rb', line 20

def type
  self.class.type
end