Class: Shaf::Generator::Forms
- Inherits:
-
Base
- Object
- Base
- Shaf::Generator::Forms
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
Instance Method Details
#call ⇒ Object
9
10
11
|
# File 'lib/shaf/generator/forms.rb', line 9
def call
create_forms
end
|
#class_name ⇒ Object
13
14
15
|
# File 'lib/shaf/generator/forms.rb', line 13
def class_name
"#{model_class_name}Forms"
end
|
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)
write_output(target, content)
end
|
#fields ⇒ Object
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
|
#opts ⇒ Object
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_dir ⇒ Object
21
22
23
|
# File 'lib/shaf/generator/forms.rb', line 21
def target_dir
'api/forms'
end
|
#target_name ⇒ Object
25
26
27
|
# File 'lib/shaf/generator/forms.rb', line 25
def target_name
"#{name}_forms.rb"
end
|
#template ⇒ Object
17
18
19
|
# File 'lib/shaf/generator/forms.rb', line 17
def template
'api/forms.rb'
end
|