Class: Shaf::Generator::Forms

Inherits:
Base
  • Object
show all
Defined in:
lib/shaf/generator/forms.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #options

Instance Method Summary collapse

Methods inherited from Base

identified_by, identifier, #identifier, inherited, #initialize, options, usage

Constructor Details

This class inherits a constructor from Shaf::Generator::Base

Instance Method Details

#callObject



9
10
11
# File 'lib/shaf/generator/forms.rb', line 9

def call
  create_forms
end

#class_nameObject



13
14
15
# File 'lib/shaf/generator/forms.rb', line 13

def class_name
  "#{model_class_name}Forms"
end

#create_formsObject



29
30
31
32
33
34
# File 'lib/shaf/generator/forms.rb', line 29

def create_forms
  content = render(template, opts)
  content = wrap_in_module(content, module_name)
  # FIXME: Append if file exists!
  write_output(target, content)
end

#fieldsObject



45
46
47
48
49
50
51
# File 'lib/shaf/generator/forms.rb', line 45

def fields
  args[1..-1].map do |f|
    (name, type, label) = f.split(':')
    label_str = label ? %(, label: "#{label}") : ''
    format 'field :%s, type: "%s"%s' % [name, rewrite(type), label_str]
  end
end

#optsObject



36
37
38
39
40
41
42
43
# File 'lib/shaf/generator/forms.rb', line 36

def opts
  {
    model_name: name,
    class_name: class_name,
    model_class_name: model_class_name,
    fields: fields
  }
end

#rewrite(type) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/shaf/generator/forms.rb', line 53

def rewrite(type)
  case type
  when /foreign_key/
    'integer'
  when NilClass
    'string'
  else
    type
  end
end

#target_dirObject



21
22
23
# File 'lib/shaf/generator/forms.rb', line 21

def target_dir
  'api/forms'
end

#target_nameObject



25
26
27
# File 'lib/shaf/generator/forms.rb', line 25

def target_name
  "#{name}_forms.rb"
end

#templateObject



17
18
19
# File 'lib/shaf/generator/forms.rb', line 17

def template
  'api/forms.rb'
end