Module: ModelForm

Defined in:
lib/model_form.rb

Constant Summary collapse

CONFIG_FILE =
"/config/model_form.yml"

Instance Method Summary collapse

Instance Method Details

#allowed_attributes(resource) ⇒ Object



38
39
40
41
# File 'lib/model_form.rb', line 38

def allowed_attributes(resource)
  config = load_config
  config.fetch(resource.class.to_s.downcase, {}).fetch("allowed_attributes", [])
end


19
20
21
22
# File 'lib/model_form.rb', line 19

def cancel_link resource
  link_to t('.cancel', :default => t("helpers.links.cancel")),
    send(resource.model_name.collection + '_path'), :class => 'btn btn-default'
end

#global_config(resource, config) ⇒ Object



29
30
31
32
# File 'lib/model_form.rb', line 29

def global_config resource, config
  # config.fetch("global_ignored_attributes", [])
  config.fetch("global", {}).fetch("ignored_attributes", [])
end

#ignored_attributes(resource) ⇒ Object



24
25
26
27
# File 'lib/model_form.rb', line 24

def ignored_attributes resource
  config = load_config
  (global_config(resource, config) + model_config(resource, config)).uniq
end

#load_configObject



43
44
45
46
47
48
49
50
51
52
# File 'lib/model_form.rb', line 43

def load_config
  file = "#{Rails.root}#{CONFIG_FILE}"
  base_file = "../generators/templates/model_form.yml"
  if File.exists?(file)
    YAML.load_file(file)
  else
    YAML.load_file File.expand_path(base_file, __FILE__)
  end

end

#model_config(resource, config) ⇒ Object



34
35
36
# File 'lib/model_form.rb', line 34

def model_config resource, config
  config.fetch(resource.class.to_s.downcase, {}).fetch("ignored_attributes", [])
end

#model_form(resource, options = {}) ⇒ Object

takes ActiveRecord object



5
6
7
8
9
10
11
12
13
# File 'lib/model_form.rb', line 5

def model_form resource, options={}
  default_options = {:html => { :class => 'form-horizontal' }}
  default_options.deep_merge! options
  simple_form_for resource, default_options do |f|
    visible_fields(resource).each {|attr_name| concat f.input(attr_name) }
    concat f.button(:submit)
    concat cancel_link(resource)
  end
end

#visible_fields(resource) ⇒ Object



15
16
17
# File 'lib/model_form.rb', line 15

def visible_fields resource
  (resource.attribute_names - ignored_attributes(resource)) | allowed_attributes(resource)
end