inline_forms 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
- data/app/helpers/form_elements/associated.rb +91 -0
- data/app/helpers/form_elements/check_box.rb +15 -0
- data/app/helpers/form_elements/checklist.rb +35 -0
- data/app/helpers/form_elements/date.rb +13 -0
- data/app/helpers/form_elements/dropdown.rb +17 -0
- data/app/helpers/form_elements/dropdown_with_values.rb +14 -0
- data/app/helpers/form_elements/geo_code_curacao.rb +16 -0
- data/app/helpers/form_elements/range.rb +14 -0
- data/app/helpers/form_elements/text_area.rb +12 -0
- data/app/helpers/form_elements/text_field.rb +13 -0
- data/app/helpers/inline_forms_helper.rb +11 -222
- data/app/views/inline_forms/_new.html.erb +1 -1
- data/app/views/inline_forms/_subform.html.erb +1 -1
- data/inline_forms.gemspec +12 -2
- data/lib/generators/inline_forms/inline_forms_generator.rb +72 -36
- data/lib/generators/inline_forms/templates/migration.rb +3 -3
- data/lib/generators/inline_forms/templates/model.rb +4 -4
- data/lib/inline_forms.rb +5 -5
- metadata +14 -4
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.
|
|
1
|
+
0.5.0
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# associated
|
|
3
|
+
def associated_show(object, attribute, values)
|
|
4
|
+
#show a list of records
|
|
5
|
+
out = ""
|
|
6
|
+
if @sub_id && @sub_id.to_i > 0
|
|
7
|
+
# if it's not a new record (sub_id > 0) then just update the list-element
|
|
8
|
+
out << '<li>'
|
|
9
|
+
out << link_to( @associated_record.title,
|
|
10
|
+
send('edit_' + @Klass.to_s.underscore + '_path', object,
|
|
11
|
+
:field => attribute,
|
|
12
|
+
:sub_id => @sub_id,
|
|
13
|
+
:form_element => this_method.reverse.sub(/.*_/,'').reverse,
|
|
14
|
+
:values => values,
|
|
15
|
+
:update => "field_#{attribute.singularize}_#{@sub_id.to_s}" ),
|
|
16
|
+
:method => :get,
|
|
17
|
+
:remote => true )
|
|
18
|
+
out << '</li>'
|
|
19
|
+
else
|
|
20
|
+
# if it's a new record (sub_id == 0) then update the whole <ul> and redraw all list-elements
|
|
21
|
+
out << "<ul class='associated #{attribute}' id='list_#{attribute}_#{object.id.to_s}'>" if @sub_id.nil?
|
|
22
|
+
if not object.send(attribute.pluralize).empty?
|
|
23
|
+
# if there are things to show, show them
|
|
24
|
+
object.send(attribute.pluralize).each do |m|
|
|
25
|
+
out << "<span id='field_#{attribute.singularize}_#{m.id.to_s}'>"
|
|
26
|
+
out << '<li>'
|
|
27
|
+
out << link_to( m.title, send('edit_' + @Klass.to_s.underscore + '_path',
|
|
28
|
+
object,
|
|
29
|
+
:field => attribute,
|
|
30
|
+
:sub_id => m.id,
|
|
31
|
+
:form_element => this_method.sub(/_[a-z]+$/,''),
|
|
32
|
+
:values => values,
|
|
33
|
+
:update => "field_#{attribute.singularize}_#{m.id.to_s}" ),
|
|
34
|
+
:method => :get,
|
|
35
|
+
:remote => true )
|
|
36
|
+
out << '</li>'
|
|
37
|
+
out << '</span>'
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
# add a 'new' link for creating a new record
|
|
41
|
+
out << '<li>'
|
|
42
|
+
out << link_to( 'new', send('edit_' + @Klass.to_s.underscore + '_path',
|
|
43
|
+
object,
|
|
44
|
+
:field => attribute,
|
|
45
|
+
:sub_id => 0,
|
|
46
|
+
:form_element => this_method.sub(/_[a-z]+$/,''),
|
|
47
|
+
:values => values,
|
|
48
|
+
:update => "list_#{attribute}_#{object.id.to_s}" ),
|
|
49
|
+
:method => :get,
|
|
50
|
+
:remote => true )
|
|
51
|
+
out << '</li>'
|
|
52
|
+
out << '</ul>' if @sub_id.nil?
|
|
53
|
+
end
|
|
54
|
+
raw(out)
|
|
55
|
+
end
|
|
56
|
+
def associated_edit(object, attribute, values)
|
|
57
|
+
# @sub_id is the id of the associated record
|
|
58
|
+
if @sub_id.to_i > 0
|
|
59
|
+
# only if @sub_id > 0, means we have a associated record
|
|
60
|
+
@associated_record_id = object.send(attribute.singularize + "_ids").index(@sub_id.to_i)
|
|
61
|
+
@associated_record = object.send(attribute)[@associated_record_id]
|
|
62
|
+
@update_span = "field_#{attribute.singularize}_#{@sub_id.to_s}"
|
|
63
|
+
else
|
|
64
|
+
# but if @sub_id = 0, then we are dealing with a new associated record
|
|
65
|
+
# in that case, we .new a record, and the update_span is the whole <ul>
|
|
66
|
+
@associated_record = attribute.singularize.capitalize.constantize.new
|
|
67
|
+
@update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
|
|
68
|
+
end
|
|
69
|
+
render :partial => "inline_forms/subform"
|
|
70
|
+
end
|
|
71
|
+
def associated_update(object, attribute, values)
|
|
72
|
+
return if object.id.nil?
|
|
73
|
+
if @sub_id.to_i > 0
|
|
74
|
+
# get the existing associated record
|
|
75
|
+
@associated_record_id = object.send(attribute.singularize + "_ids").index(@sub_id.to_i)
|
|
76
|
+
@associated_record = object.send(attribute)[@associated_record_id]
|
|
77
|
+
@update_span = "field_" + attribute.singularize + '_' + @sub_id.to_s
|
|
78
|
+
else
|
|
79
|
+
# create a new associated record
|
|
80
|
+
@associated_record = object.send(attribute.to_sym).new
|
|
81
|
+
@update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
|
|
82
|
+
end
|
|
83
|
+
# process the sub_form fields (attributes). These are declared in the model!
|
|
84
|
+
@associated_record.inline_forms_field_list.each do | @subform_description, @subform_field, @subform_element |
|
|
85
|
+
# have no fear
|
|
86
|
+
send("#{@subform_element}_update", @associated_record, @subform_field, nil)
|
|
87
|
+
end
|
|
88
|
+
@associated_record.save
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# boolean, bit unaptly named check_box
|
|
3
|
+
def check_box_show(object, attribute, values)
|
|
4
|
+
values ||= { 'false' => 'no', 'true' => 'yes' }
|
|
5
|
+
link_to_inline_edit object, attribute, values[object.send(attribute).to_s], values
|
|
6
|
+
end
|
|
7
|
+
def check_box_edit(object, attribute, values)
|
|
8
|
+
values ||= { 'false' => 'no', 'true' => 'yes' }
|
|
9
|
+
collection_select( object.class.to_s.downcase, attribute, values, 'first', 'last', :selected => object.send(attribute).to_s)
|
|
10
|
+
end
|
|
11
|
+
def check_box_update(object, attribute, values)
|
|
12
|
+
object[attribute.to_s.to_sym] = params[object.class.to_s.downcase.to_sym][attribute.to_s.to_sym]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# checklist
|
|
3
|
+
def checklist_show(object, attribute, values)
|
|
4
|
+
out = '<ul class="checklist">'
|
|
5
|
+
out << link_to_inline_edit(object, attribute, nil, nil) if object.send(attribute).empty?
|
|
6
|
+
object.send(attribute).sort.each do | item |
|
|
7
|
+
out << '<li>'
|
|
8
|
+
out << link_to_inline_edit(object, attribute, item.title, nil)
|
|
9
|
+
out << '</li>'
|
|
10
|
+
end
|
|
11
|
+
out << '</ul>'
|
|
12
|
+
end
|
|
13
|
+
def checklist_edit(object, attribute, values)
|
|
14
|
+
object.send(attribute).build if object.send(attribute).empty?
|
|
15
|
+
values = object.send(attribute).first.class.name.constantize.find(:all) # TODO bring order
|
|
16
|
+
out = '<div class="edit_form_checklist">'
|
|
17
|
+
out << '<ul>'
|
|
18
|
+
values.each do | item |
|
|
19
|
+
out << '<li>'
|
|
20
|
+
out << check_box_tag( attribute + '[' + item.id.to_s + ']', 'yes', object.send(attribute.singularize + "_ids").include?(item.id) )
|
|
21
|
+
out << '<div class="edit_form_checklist_text">'
|
|
22
|
+
out << h(item.title)
|
|
23
|
+
out << '</div>'
|
|
24
|
+
out << '<div style="clear: both;"></div>'
|
|
25
|
+
out << '</li>'
|
|
26
|
+
end
|
|
27
|
+
out << '</ul>'
|
|
28
|
+
out << '</div>'
|
|
29
|
+
end
|
|
30
|
+
def checklist_update(object, attribute, values)
|
|
31
|
+
params[attribute] ||= {}
|
|
32
|
+
object.send(attribute.singularize + '_ids=', params[attribute].keys)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# date
|
|
3
|
+
def date_show(object, attribute, values)
|
|
4
|
+
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
5
|
+
end
|
|
6
|
+
def date_edit(object, attribute, values)
|
|
7
|
+
calendar_date_select_tag attribute, object[attribute], :year_range => 30.years.ago..5.years.from_now, :popup => :force
|
|
8
|
+
end
|
|
9
|
+
def date_update(object, attribute, values)
|
|
10
|
+
object[attribute.to_sym] = params[attribute.to_sym]
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# dropdown
|
|
3
|
+
def dropdown_show(object, attribute, values)
|
|
4
|
+
attribute_value = object.send(attribute).presentation rescue nil
|
|
5
|
+
link_to_inline_edit object, attribute, attribute_value, nil
|
|
6
|
+
end
|
|
7
|
+
def dropdown_edit(object, attribute, values)
|
|
8
|
+
object.send('build_' + attribute.to_s) unless object.send(attribute)
|
|
9
|
+
values = object.send(attribute).class.name.constantize.find(:all) # TODO bring order!
|
|
10
|
+
# the leading underscore is to avoid name conflicts, like 'email' and 'email_type' will result in 'email' and 'email[email_type_id]' in the form!
|
|
11
|
+
collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_s.foreign_key.to_sym, values, 'id', 'presentation', :selected => object.send(attribute).id)
|
|
12
|
+
end
|
|
13
|
+
def dropdown_update(object, attribute, values)
|
|
14
|
+
object[attribute.to_s.foreign_key.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_s.foreign_key.to_sym]
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# dropdown_with_values
|
|
3
|
+
def dropdown_with_values_show(object, attribute, values)
|
|
4
|
+
link_to_inline_edit object, attribute, values[object.send(attribute)], values
|
|
5
|
+
end
|
|
6
|
+
def dropdown_with_values_edit(object, attribute, values)
|
|
7
|
+
# the leading underscore is to avoid name conflicts, like 'email' and 'email_type' will result in 'email' and 'email[email_type_id]' in the form!
|
|
8
|
+
collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
|
|
9
|
+
end
|
|
10
|
+
def dropdown_with_values_update(object, attribute, values)
|
|
11
|
+
object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# geo_code_curacao
|
|
3
|
+
def geo_code_curacao_show(object, attribute, values)
|
|
4
|
+
attribute_value = object.send(attribute).presentation rescue nil
|
|
5
|
+
link_to_inline_edit object, attribute, attribute_value, nil
|
|
6
|
+
end
|
|
7
|
+
def geo_code_curacao_edit(object, attribute, values)
|
|
8
|
+
text_field_with_auto_complete :geo_code_curacao, :street, :skip_style => true
|
|
9
|
+
end
|
|
10
|
+
def geo_code_curacao_update(object, attribute, values)
|
|
11
|
+
# extract the geocode
|
|
12
|
+
geo_code = params[attribute.to_sym][:street].scan(/\d\d\d\d\d\d/).to_s || nil
|
|
13
|
+
object[attribute.to_sym] = GeoCodeCuracao.new(geo_code).valid? ? geo_code : nil
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# range
|
|
3
|
+
def range_show(object, attribute, values)
|
|
4
|
+
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
5
|
+
end
|
|
6
|
+
def range_edit(object, attribute, values)
|
|
7
|
+
# the leading underscore is to avoid name conflicts, like 'email' and 'email_type' will result in 'email' and 'email[email_type_id]' in the form!
|
|
8
|
+
collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'to_i', 'to_s', :selected => object.send(attribute))
|
|
9
|
+
end
|
|
10
|
+
def range_update(object, attribute, values)
|
|
11
|
+
object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# text_area
|
|
3
|
+
def text_area_show(object, attribute, values)
|
|
4
|
+
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
5
|
+
end
|
|
6
|
+
def text_area_edit(object, attribute, values)
|
|
7
|
+
text_area_tag attribute, object[attribute], :class => 'field_text_area'
|
|
8
|
+
end
|
|
9
|
+
def text_area_update(object, attribute, values)
|
|
10
|
+
object[attribute.to_sym] = params[attribute.to_sym]
|
|
11
|
+
end
|
|
12
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module InlineFormsHelper
|
|
2
|
+
# text_field
|
|
3
|
+
def text_field_show(object, attribute, values)
|
|
4
|
+
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
5
|
+
end
|
|
6
|
+
def text_field_edit(object, attribute, values)
|
|
7
|
+
text_field_tag attribute, object[attribute], :class => 'input_text_field'
|
|
8
|
+
end
|
|
9
|
+
def text_field_update(object, attribute, values)
|
|
10
|
+
object[attribute.to_sym] = params[attribute.to_sym]
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
INLINE_FORMS_PATH = File.dirname(__FILE__) + "/form_elements/"
|
|
2
|
+
|
|
3
|
+
Dir[INLINE_FORMS_PATH + "*.rb"].each do |form_element|
|
|
4
|
+
require form_element
|
|
5
|
+
end
|
|
6
|
+
|
|
1
7
|
module InlineFormsHelper
|
|
2
8
|
# display the forms from an array of attributes
|
|
3
9
|
def inline_form_display(object, attributes, action=:show)
|
|
@@ -5,7 +11,7 @@ module InlineFormsHelper
|
|
|
5
11
|
out = String.new #ugly as hell but that's how content_tag works...
|
|
6
12
|
case action
|
|
7
13
|
when :show
|
|
8
|
-
attributes.each do |
|
|
14
|
+
attributes.each do | attribute, name, form_element, values |
|
|
9
15
|
#css_class_id = form_element == :associated ? "subform_#{attribute.to_s}_#{object.id}" : "field_#{attribute.to_s}_#{object.id}"
|
|
10
16
|
css_class_id = "field_#{attribute.to_s}_#{object.id}"
|
|
11
17
|
name_cell = content_tag :td, :valign=>'top' do
|
|
@@ -25,7 +31,7 @@ module InlineFormsHelper
|
|
|
25
31
|
end
|
|
26
32
|
return content_tag :table, raw(out), :cellspacing => 0, :cellpadding => 0
|
|
27
33
|
when :new
|
|
28
|
-
attributes.each do |
|
|
34
|
+
attributes.each do | attribute, name, form_element, values |
|
|
29
35
|
#css_class_id = form_element == :associated ? "subform_#{attribute.to_s}_#{object.id}" : "field_#{attribute.to_s}_#{object.id}"
|
|
30
36
|
if not form_element.to_sym == :associated
|
|
31
37
|
css_class_id = "field_#{attribute.to_s}_#{object.id}"
|
|
@@ -52,235 +58,18 @@ module InlineFormsHelper
|
|
|
52
58
|
t = ''
|
|
53
59
|
objects.each do |object|
|
|
54
60
|
t += content_tag tag do
|
|
55
|
-
inline_form_display object, object.respond_to?(:inline_forms_field_list) ? object.inline_forms_field_list : [
|
|
61
|
+
inline_form_display object, object.respond_to?(:inline_forms_field_list) ? object.inline_forms_field_list : [ :name, 'name', 'text' ]
|
|
56
62
|
end
|
|
57
63
|
end
|
|
58
64
|
return raw(t)
|
|
59
65
|
end
|
|
60
66
|
# link for new item
|
|
61
|
-
def inline_form_new_record(attribute, form_element, text='
|
|
67
|
+
def inline_form_new_record(attribute, form_element, text='new', update_span='inline_form_list')
|
|
62
68
|
link_to text, send('new_' + @Klass.to_s.underscore + '_path', :update => update_span), :remote => true
|
|
63
69
|
end
|
|
64
70
|
|
|
65
|
-
# dropdown
|
|
66
|
-
def dropdown_show(object, attribute, values)
|
|
67
|
-
attribute_value = object.send(attribute).presentation rescue nil
|
|
68
|
-
link_to_inline_edit object, attribute, attribute_value, nil
|
|
69
|
-
end
|
|
70
|
-
def dropdown_edit(object, attribute, values)
|
|
71
|
-
object.send('build_' + attribute.to_s) unless object.send(attribute)
|
|
72
|
-
values = object.send(attribute).class.name.constantize.find(:all) # TODO bring order!
|
|
73
|
-
# the leading underscore is to avoid name conflicts, like 'email' and 'email_type' will result in 'email' and 'email[email_type_id]' in the form!
|
|
74
|
-
collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_s.foreign_key.to_sym, values, 'id', 'presentation', :selected => object.send(attribute).id)
|
|
75
|
-
end
|
|
76
|
-
def dropdown_update(object, attribute, values)
|
|
77
|
-
object[attribute.to_s.foreign_key.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_s.foreign_key.to_sym]
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
# dropdown_with_values
|
|
81
|
-
def dropdown_with_values_show(object, attribute, values)
|
|
82
|
-
link_to_inline_edit object, attribute, values[object.send(attribute)], values
|
|
83
|
-
end
|
|
84
|
-
def dropdown_with_values_edit(object, attribute, values)
|
|
85
|
-
# the leading underscore is to avoid name conflicts, like 'email' and 'email_type' will result in 'email' and 'email[email_type_id]' in the form!
|
|
86
|
-
collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
|
|
87
|
-
end
|
|
88
|
-
def dropdown_with_values_update(object, attribute, values)
|
|
89
|
-
object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
# range
|
|
93
|
-
def range_show(object, attribute, values)
|
|
94
|
-
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
95
|
-
end
|
|
96
|
-
def range_edit(object, attribute, values)
|
|
97
|
-
# the leading underscore is to avoid name conflicts, like 'email' and 'email_type' will result in 'email' and 'email[email_type_id]' in the form!
|
|
98
|
-
collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'to_i', 'to_s', :selected => object.send(attribute))
|
|
99
|
-
end
|
|
100
|
-
def range_update(object, attribute, values)
|
|
101
|
-
object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
# date
|
|
105
|
-
def date_show(object, attribute, values)
|
|
106
|
-
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
107
|
-
end
|
|
108
|
-
def date_edit(object, attribute, values)
|
|
109
|
-
calendar_date_select_tag attribute, object[attribute], :year_range => 30.years.ago..5.years.from_now, :popup => :force
|
|
110
|
-
end
|
|
111
|
-
def date_update(object, attribute, values)
|
|
112
|
-
object[attribute.to_sym] = params[attribute.to_sym]
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
# textarea
|
|
116
|
-
def textarea_show(object, attribute, values)
|
|
117
|
-
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
118
|
-
end
|
|
119
|
-
def textarea_edit(object, attribute, values)
|
|
120
|
-
text_area_tag attribute, object[attribute], :class => 'field_textarea'
|
|
121
|
-
end
|
|
122
|
-
def textarea_update(object, attribute, values)
|
|
123
|
-
object[attribute.to_sym] = params[attribute.to_sym]
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
# text
|
|
127
|
-
def text_show(object, attribute, values)
|
|
128
|
-
link_to_inline_edit object, attribute, object.send(attribute), nil
|
|
129
|
-
end
|
|
130
|
-
def text_edit(object, attribute, values)
|
|
131
|
-
text_field_tag attribute, object[attribute], :class => 'input_text'
|
|
132
|
-
end
|
|
133
|
-
def text_update(object, attribute, values)
|
|
134
|
-
object[attribute.to_sym] = params[attribute.to_sym]
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
# bool
|
|
138
|
-
def bool_show(object, attribute, values)
|
|
139
|
-
link_to_inline_edit object, attribute, values[object.send(attribute).to_s], values
|
|
140
|
-
end
|
|
141
|
-
def bool_edit(object, attribute, values)
|
|
142
|
-
collection_select( object.class.to_s.downcase, attribute, values, 'first', 'last', :selected => object.send(attribute).to_s)
|
|
143
|
-
end
|
|
144
|
-
def bool_update(object, attribute, values)
|
|
145
|
-
object[attribute.to_s.to_sym] = params[object.class.to_s.downcase.to_sym][attribute.to_s.to_sym]
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
# checklist
|
|
149
|
-
def checklist_show(object, attribute, values)
|
|
150
|
-
out = '<ul class="checklist">'
|
|
151
|
-
out << link_to_inline_edit(object, attribute, nil, nil) if object.send(attribute).empty?
|
|
152
|
-
object.send(attribute).sort.each do | item |
|
|
153
|
-
out << '<li>'
|
|
154
|
-
out << link_to_inline_edit(object, attribute, item.title, nil)
|
|
155
|
-
out << '</li>'
|
|
156
|
-
end
|
|
157
|
-
out << '</ul>'
|
|
158
|
-
end
|
|
159
|
-
def checklist_edit(object, attribute, values)
|
|
160
|
-
object.send(attribute).build if object.send(attribute).empty?
|
|
161
|
-
values = object.send(attribute).first.class.name.constantize.find(:all) # TODO bring order
|
|
162
|
-
out = '<div class="edit_form_checklist">'
|
|
163
|
-
out << '<ul>'
|
|
164
|
-
values.each do | item |
|
|
165
|
-
out << '<li>'
|
|
166
|
-
out << check_box_tag( attribute + '[' + item.id.to_s + ']', 'yes', object.send(attribute.singularize + "_ids").include?(item.id) )
|
|
167
|
-
out << '<div class="edit_form_checklist_text">'
|
|
168
|
-
out << h(item.title)
|
|
169
|
-
out << '</div>'
|
|
170
|
-
out << '<div style="clear: both;"></div>'
|
|
171
|
-
out << '</li>'
|
|
172
|
-
end
|
|
173
|
-
out << '</ul>'
|
|
174
|
-
out << '</div>'
|
|
175
|
-
end
|
|
176
|
-
def checklist_update(object, attribute, values)
|
|
177
|
-
params[attribute] ||= {}
|
|
178
|
-
object.send(attribute.singularize + '_ids=', params[attribute].keys)
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
# associated
|
|
182
|
-
def associated_show(object, attribute, values)
|
|
183
|
-
#show a list of records
|
|
184
|
-
out = ""
|
|
185
|
-
if @sub_id && @sub_id.to_i > 0
|
|
186
|
-
# if it's not a new record (sub_id > 0) then just update the list-element
|
|
187
|
-
out << '<li>'
|
|
188
|
-
out << link_to( @associated_record.title,
|
|
189
|
-
send('edit_' + @Klass.to_s.underscore + '_path', object,
|
|
190
|
-
:field => attribute,
|
|
191
|
-
:sub_id => @sub_id,
|
|
192
|
-
:form_element => this_method.reverse.sub(/.*_/,'').reverse,
|
|
193
|
-
:values => values,
|
|
194
|
-
:update => "field_#{attribute.singularize}_#{@sub_id.to_s}" ),
|
|
195
|
-
:method => :get,
|
|
196
|
-
:remote => true )
|
|
197
|
-
out << '</li>'
|
|
198
|
-
else
|
|
199
|
-
# if it's a new record (sub_id == 0) then update the whole <ul> and redraw all list-elements
|
|
200
|
-
out << "<ul class='associated #{attribute}' id='list_#{attribute}_#{object.id.to_s}'>" if @sub_id.nil?
|
|
201
|
-
if not object.send(attribute.pluralize).empty?
|
|
202
|
-
# if there are things to show, show them
|
|
203
|
-
object.send(attribute.pluralize).each do |m|
|
|
204
|
-
out << "<span id='field_#{attribute.singularize}_#{m.id.to_s}'>"
|
|
205
|
-
out << '<li>'
|
|
206
|
-
out << link_to( m.title, send('edit_' + @Klass.to_s.underscore + '_path',
|
|
207
|
-
object,
|
|
208
|
-
:field => attribute,
|
|
209
|
-
:sub_id => m.id,
|
|
210
|
-
:form_element => this_method.sub(/_[a-z]+$/,''),
|
|
211
|
-
:values => values,
|
|
212
|
-
:update => "field_#{attribute.singularize}_#{m.id.to_s}" ),
|
|
213
|
-
:method => :get,
|
|
214
|
-
:remote => true )
|
|
215
|
-
out << '</li>'
|
|
216
|
-
out << '</span>'
|
|
217
|
-
end
|
|
218
|
-
end
|
|
219
|
-
# add a 'new' link for creating a new record
|
|
220
|
-
out << '<li>'
|
|
221
|
-
out << link_to( 'new', send('edit_' + @Klass.to_s.underscore + '_path',
|
|
222
|
-
object,
|
|
223
|
-
:field => attribute,
|
|
224
|
-
:sub_id => 0,
|
|
225
|
-
:form_element => this_method.sub(/_[a-z]+$/,''),
|
|
226
|
-
:values => values,
|
|
227
|
-
:update => "list_#{attribute}_#{object.id.to_s}" ),
|
|
228
|
-
:method => :get,
|
|
229
|
-
:remote => true )
|
|
230
|
-
out << '</li>'
|
|
231
|
-
out << '</ul>' if @sub_id.nil?
|
|
232
|
-
end
|
|
233
|
-
raw(out)
|
|
234
|
-
end
|
|
235
|
-
def associated_edit(object, attribute, values)
|
|
236
|
-
# @sub_id is the id of the associated record
|
|
237
|
-
if @sub_id.to_i > 0
|
|
238
|
-
# only if @sub_id > 0, means we have a associated record
|
|
239
|
-
@associated_record_id = object.send(attribute.singularize + "_ids").index(@sub_id.to_i)
|
|
240
|
-
@associated_record = object.send(attribute)[@associated_record_id]
|
|
241
|
-
@update_span = "field_#{attribute.singularize}_#{@sub_id.to_s}"
|
|
242
|
-
else
|
|
243
|
-
# but if @sub_id = 0, then we are dealing with a new associated record
|
|
244
|
-
# in that case, we .new a record, and the update_span is the whole <ul>
|
|
245
|
-
@associated_record = attribute.singularize.capitalize.constantize.new
|
|
246
|
-
@update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
|
|
247
|
-
end
|
|
248
|
-
render :partial => "inline_forms/subform"
|
|
249
|
-
end
|
|
250
|
-
def associated_update(object, attribute, values)
|
|
251
|
-
return if object.id.nil?
|
|
252
|
-
if @sub_id.to_i > 0
|
|
253
|
-
# get the existing associated record
|
|
254
|
-
@associated_record_id = object.send(attribute.singularize + "_ids").index(@sub_id.to_i)
|
|
255
|
-
@associated_record = object.send(attribute)[@associated_record_id]
|
|
256
|
-
@update_span = "field_" + attribute.singularize + '_' + @sub_id.to_s
|
|
257
|
-
else
|
|
258
|
-
# create a new associated record
|
|
259
|
-
@associated_record = object.send(attribute.to_sym).new
|
|
260
|
-
@update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
|
|
261
|
-
end
|
|
262
|
-
# process the sub_form fields (attributes). These are declared in the model!
|
|
263
|
-
@associated_record.inline_forms_field_list.each do | @subform_description, @subform_field, @subform_element |
|
|
264
|
-
# have no fear
|
|
265
|
-
send("#{@subform_element}_update", @associated_record, @subform_field, nil)
|
|
266
|
-
end
|
|
267
|
-
@associated_record.save
|
|
268
|
-
end
|
|
269
|
-
|
|
270
|
-
# geo_code_curacao
|
|
271
|
-
def geo_code_curacao_show(object, attribute, values)
|
|
272
|
-
attribute_value = object.send(attribute).presentation rescue nil
|
|
273
|
-
link_to_inline_edit object, attribute, attribute_value, nil
|
|
274
|
-
end
|
|
275
|
-
def geo_code_curacao_edit(object, attribute, values)
|
|
276
|
-
text_field_with_auto_complete :geo_code_curacao, :street, :skip_style => true
|
|
277
|
-
end
|
|
278
|
-
def geo_code_curacao_update(object, attribute, values)
|
|
279
|
-
# extract the geocode
|
|
280
|
-
geo_code = params[attribute.to_sym][:street].scan(/\d\d\d\d\d\d/).to_s || nil
|
|
281
|
-
object[attribute.to_sym] = GeoCodeCuracao.new(geo_code).valid? ? geo_code : nil
|
|
282
|
-
end
|
|
283
71
|
|
|
72
|
+
|
|
284
73
|
private
|
|
285
74
|
|
|
286
75
|
# link_to_inline_edit
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<% form_tag send(@Klass.to_s.underscore.pluralize + '_path', :update => @update_span || 'inline_form_list' ),
|
|
2
2
|
:multipart => true, :remote => true, :class => "edit_form" do -%>
|
|
3
3
|
<div class="edit_form_field">
|
|
4
|
-
<%= inline_form_display(@object, @object.respond_to?(:inline_forms_field_list) ? @object.inline_forms_field_list : [
|
|
4
|
+
<%= inline_form_display(@object, @object.respond_to?(:inline_forms_field_list) ? @object.inline_forms_field_list : [ :name, 'name', :text ], :new) %>
|
|
5
5
|
</div>
|
|
6
6
|
<%= link_to( send(@Klass.to_s.underscore.pluralize + '_path',:update => @update_span || 'inline_form_list'), :remote => true,
|
|
7
7
|
:class => "edit_form_cancel" ) do %>
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
<table cellpadding="0" cellspacing="0" class="subform">
|
|
2
|
-
<% @associated_record.inline_forms_field_list.each do | @
|
|
2
|
+
<% @associated_record.inline_forms_field_list.each do | @subform_field, @subform_description, @subform_element, @values | %>
|
|
3
3
|
<tr>
|
|
4
4
|
<td valign="top">
|
|
5
5
|
<div class="subform_field-name">
|
data/inline_forms.gemspec
CHANGED
|
@@ -5,11 +5,11 @@
|
|
|
5
5
|
|
|
6
6
|
Gem::Specification.new do |s|
|
|
7
7
|
s.name = %q{inline_forms}
|
|
8
|
-
s.version = "0.
|
|
8
|
+
s.version = "0.5.0"
|
|
9
9
|
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
11
11
|
s.authors = ["Ace Suares"]
|
|
12
|
-
s.date = %q{2011-
|
|
12
|
+
s.date = %q{2011-02-03}
|
|
13
13
|
s.description = %q{Inline Forms aims to ease the setup of forms that provide inline editing. The field list can be specified in the model.}
|
|
14
14
|
s.email = %q{ace@suares.an}
|
|
15
15
|
s.extra_rdoc_files = [
|
|
@@ -25,6 +25,16 @@ Gem::Specification.new do |s|
|
|
|
25
25
|
"Rakefile",
|
|
26
26
|
"VERSION",
|
|
27
27
|
"app/controllers/inline_forms_controller.rb",
|
|
28
|
+
"app/helpers/form_elements/associated.rb",
|
|
29
|
+
"app/helpers/form_elements/check_box.rb",
|
|
30
|
+
"app/helpers/form_elements/checklist.rb",
|
|
31
|
+
"app/helpers/form_elements/date.rb",
|
|
32
|
+
"app/helpers/form_elements/dropdown.rb",
|
|
33
|
+
"app/helpers/form_elements/dropdown_with_values.rb",
|
|
34
|
+
"app/helpers/form_elements/geo_code_curacao.rb",
|
|
35
|
+
"app/helpers/form_elements/range.rb",
|
|
36
|
+
"app/helpers/form_elements/text_area.rb",
|
|
37
|
+
"app/helpers/form_elements/text_field.rb",
|
|
28
38
|
"app/helpers/inline_forms_helper.rb",
|
|
29
39
|
"app/models/geo_code_curacao.rb",
|
|
30
40
|
"app/models/inline_form.rb",
|
|
@@ -1,49 +1,85 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
module InlineForms
|
|
2
|
+
class InlineFormsGenerator < Rails::Generators::NamedBase
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
Rails::Generators::GeneratedAttribute.class_eval do
|
|
5
|
+
def migration_type
|
|
6
|
+
# convert our form_elements to real types for migration
|
|
7
|
+
case type
|
|
8
|
+
# normal types don't get converted
|
|
9
|
+
when :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean then type
|
|
10
|
+
# our types get converted
|
|
11
|
+
when :dropdown, :dropdown_with_values then :integer
|
|
12
|
+
when :check_box, :boolean_with_values then :boolean
|
|
13
|
+
when :date then :date
|
|
14
|
+
when :text_area then :text
|
|
15
|
+
else
|
|
16
|
+
:unknown # migration will fail, probably.
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
def field_type
|
|
21
|
+
case type
|
|
22
|
+
# out types don't get converted
|
|
23
|
+
when :dropdown, :dropdown_with_values, :check_box, :boolean_with_values then type
|
|
24
|
+
when :integer, :float, :decimal then :text_field # who knows if they wanted a dropdown?
|
|
25
|
+
when :time then :time_select
|
|
26
|
+
when :datetime, :timestamp then :datetime_select
|
|
27
|
+
when :date then :date_select
|
|
28
|
+
when :text then :text_area
|
|
29
|
+
when :boolean then :check_box
|
|
30
|
+
else
|
|
31
|
+
:unkown # form will fail to generatie, probably
|
|
32
|
+
end
|
|
33
|
+
end
|
|
5
34
|
|
|
6
|
-
|
|
7
|
-
#copy_file "stylesheet.css", "public/stylesheets/#{file_name}.css"
|
|
8
|
-
template "model.rb", "app/models/#{model_file_name}.rb"
|
|
9
|
-
end
|
|
35
|
+
end
|
|
10
36
|
|
|
11
|
-
|
|
12
|
-
template "controller.rb", "app/controllers/#{controller_file_name}.rb"
|
|
13
|
-
end
|
|
37
|
+
argument :attributes, :type => :array, :banner => "field:type field:type"
|
|
14
38
|
|
|
15
|
-
|
|
16
|
-
route "resources :#{resource_name}"
|
|
17
|
-
end
|
|
39
|
+
source_root File.expand_path('../templates', __FILE__)
|
|
18
40
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
41
|
+
def generate_model
|
|
42
|
+
#copy_file "stylesheet.css", "public/stylesheets/#{file_name}.css"
|
|
43
|
+
template "model.rb", "app/models/#{model_file_name}.rb"
|
|
44
|
+
end
|
|
22
45
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
end
|
|
46
|
+
def generate_controller
|
|
47
|
+
template "controller.rb", "app/controllers/#{controller_file_name}.rb"
|
|
48
|
+
end
|
|
27
49
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
50
|
+
def generate_route
|
|
51
|
+
route "resources :#{resource_name}"
|
|
52
|
+
end
|
|
31
53
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
54
|
+
def generate_migration
|
|
55
|
+
template "migration.rb", "db/migrate/#{time_stamp}_inline_forms_create_#{table_name}.rb"
|
|
56
|
+
end
|
|
35
57
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
58
|
+
private
|
|
59
|
+
def model_file_name
|
|
60
|
+
name.underscore
|
|
61
|
+
end
|
|
39
62
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
63
|
+
def resource_name
|
|
64
|
+
name.pluralize.underscore
|
|
65
|
+
end
|
|
43
66
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
67
|
+
def controller_name
|
|
68
|
+
name.pluralize + 'Controller'
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def controller_file_name
|
|
72
|
+
controller_name.underscore
|
|
73
|
+
end
|
|
48
74
|
|
|
75
|
+
def table_name
|
|
76
|
+
name.pluralize.underscore
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def time_stamp
|
|
80
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
|
81
|
+
# found it here http://whynotwiki.com/Ruby_/_Dates_and_times
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
end
|
|
49
85
|
end
|
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
class InlineFormsCreate<%= table_name.camelize %> < ActiveRecord::Migration
|
|
2
2
|
def self.up
|
|
3
|
-
create_table
|
|
3
|
+
create_table :<%= table_name %> do |t|
|
|
4
4
|
<% for attribute in attributes -%>
|
|
5
|
-
t.<%= attribute.
|
|
5
|
+
t.<%= attribute.migration_type %> :<%= attribute.name %>
|
|
6
6
|
<% end -%>
|
|
7
7
|
t.timestamps
|
|
8
8
|
end
|
|
9
|
-
|
|
10
9
|
end
|
|
11
10
|
|
|
12
11
|
def self.down
|
|
13
12
|
drop_table :<%= table_name %>
|
|
13
|
+
end
|
|
14
14
|
end
|
|
15
15
|
#The types supported by Active Record are :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean
|
|
@@ -6,9 +6,9 @@ class <%= name %> < ActiveRecord::Base
|
|
|
6
6
|
|
|
7
7
|
def inline_forms_field_list
|
|
8
8
|
[
|
|
9
|
-
<% for attribute in attributes
|
|
10
|
-
[ '<%= attribute.name %>', :<%= attribute.
|
|
11
|
-
<% end
|
|
9
|
+
<% for attribute in attributes %>
|
|
10
|
+
[ :<%= attribute.migration_type %>, '<%= attribute.name %>', :<%= attribute.field_type %> ],
|
|
11
|
+
<% end %>
|
|
12
12
|
]
|
|
13
|
-
end
|
|
14
13
|
end
|
|
14
|
+
end
|
data/lib/inline_forms.rb
CHANGED
|
@@ -2,9 +2,9 @@ puts 'loading inline_forms...'
|
|
|
2
2
|
|
|
3
3
|
module InlineForms
|
|
4
4
|
class InlineFormsEngine < Rails::Engine
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
initializer 'inline_forms.helper' do |app|
|
|
6
|
+
ActionView::Base.send :include, InlineFormsHelper
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
10
|
# http://www.ruby-forum.com/topic/211017#927932
|
metadata
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: inline_forms
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
hash:
|
|
4
|
+
hash: 11
|
|
5
5
|
prerelease: false
|
|
6
6
|
segments:
|
|
7
7
|
- 0
|
|
8
|
-
-
|
|
8
|
+
- 5
|
|
9
9
|
- 0
|
|
10
|
-
version: 0.
|
|
10
|
+
version: 0.5.0
|
|
11
11
|
platform: ruby
|
|
12
12
|
authors:
|
|
13
13
|
- Ace Suares
|
|
@@ -15,7 +15,7 @@ autorequire:
|
|
|
15
15
|
bindir: bin
|
|
16
16
|
cert_chain: []
|
|
17
17
|
|
|
18
|
-
date: 2011-
|
|
18
|
+
date: 2011-02-03 00:00:00 -04:00
|
|
19
19
|
default_executable:
|
|
20
20
|
dependencies:
|
|
21
21
|
- !ruby/object:Gem::Dependency
|
|
@@ -96,6 +96,16 @@ files:
|
|
|
96
96
|
- Rakefile
|
|
97
97
|
- VERSION
|
|
98
98
|
- app/controllers/inline_forms_controller.rb
|
|
99
|
+
- app/helpers/form_elements/associated.rb
|
|
100
|
+
- app/helpers/form_elements/check_box.rb
|
|
101
|
+
- app/helpers/form_elements/checklist.rb
|
|
102
|
+
- app/helpers/form_elements/date.rb
|
|
103
|
+
- app/helpers/form_elements/dropdown.rb
|
|
104
|
+
- app/helpers/form_elements/dropdown_with_values.rb
|
|
105
|
+
- app/helpers/form_elements/geo_code_curacao.rb
|
|
106
|
+
- app/helpers/form_elements/range.rb
|
|
107
|
+
- app/helpers/form_elements/text_area.rb
|
|
108
|
+
- app/helpers/form_elements/text_field.rb
|
|
99
109
|
- app/helpers/inline_forms_helper.rb
|
|
100
110
|
- app/models/geo_code_curacao.rb
|
|
101
111
|
- app/models/inline_form.rb
|