Class: Filch::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/filch.rb

Overview

Templates

Builds a hash. keys ares are an array template_keys , values are Filch::Template.

@templates[template_key] = Filch::Template.new(@model, template_key)

template_keys can be defined in the model

class Foo < ApplicationRecord
  def self.filch_template_keys
    %i[all basic]
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Templates

Returns a new instance of Templates.



63
64
65
66
# File 'lib/filch.rb', line 63

def initialize(model)
  @model = model
  @templates = build_templates
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



67
68
69
# File 'lib/filch.rb', line 67

def model
  @model
end

#templatesObject (readonly)

Returns the value of attribute templates.



67
68
69
# File 'lib/filch.rb', line 67

def templates
  @templates
end

Instance Method Details

#build_templatesObject

def button_attrs

attrs = {
}
if @model.methods.include?(:button_attrs)
  attrs = attrs.merge(@model.button_attrs) || attrs
end
attrs

end



113
114
115
116
117
118
119
120
# File 'lib/filch.rb', line 113

def build_templates
  templates = {}
  templates.default = Template.new(@model, 'default')
  template_keys.each do |template|
    templates[template] = Template.new(@model, template)
  end
  templates
end

#html_attrsObject



94
95
96
97
98
99
100
101
# File 'lib/filch.rb', line 94

def html_attrs
  attrs = html_attrs_values
  attrs.default = {}
  attrs.each_key do |key|
    attrs[key] = attrs[key].merge(model_html_attrs[key])
  end
  attrs
end

#html_attrs_valuesObject



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

def html_attrs_values
  {
    assocs: { click: 'filchColClick', load: 'toggleShowHide' },
    attrs: { click: 'filchColClick', load: 'toggleShowHide' },
    button: {
      click: 'toggleShowHide', shId: 'filch', load: 'toggleShowHide'
    },
    datalist: {
      updateInner_path: '/filch/datalist', class: 'js', update: 'dlUpdate'
    },
    field: { class: 'js form-control', input: 'filchDataList' },
    label: { class: 'cheese'}
  }
end

#model_html_attrsObject



84
85
86
87
88
89
90
91
92
# File 'lib/filch.rb', line 84

def model_html_attrs
  model_attrs = if @model.methods.include?(:filch_html_attrs)
                  @model.filch_html_attrs
                else
                  {}
                end
  model_attrs.default = {}
  model_attrs
end

#template_keysObject

will generate forms based on the list of templates in the Array YourModel.template_keys. defaults to [‘all’]



125
126
127
128
129
130
131
# File 'lib/filch.rb', line 125

def template_keys
  if @model.methods.include?(:template_keys)
    @model.template_keys || ['all']
  else
    ['all']
  end
end