inline_forms 0.7.4 → 0.8.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/README.rdoc CHANGED
@@ -1,6 +1,17 @@
1
1
  = inline_forms
2
2
 
3
- Description goes here.
3
+ Ever tired of setting up forms to manage your data? Inline Forms aims at quick and easy setup of models, migrations and controllers and providing a user-friendly interface.
4
+
5
+ = Usage
6
+
7
+ Look in the generator files for a hint about usage. In short:
8
+ rails g inline_forms Person first_name:string last_name:string age:dropdown_with_integers
9
+ will create a model, a migration an a controller. In the model you would only have to add
10
+ values for the dropdown, or instance 12..45.
11
+ In going to /persons you would see a nice form to add and edit persons.
12
+
13
+ It's work in progress.
14
+
4
15
 
5
16
  == Contributing to inline_forms
6
17
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.7.4
1
+ 0.8.0
@@ -54,15 +54,14 @@ class InlineFormsController < ApplicationController
54
54
  end
55
55
  end
56
56
 
57
- # :edit presents a form to edit one specific field from an object
57
+ # :edit presents a form to edit one specific attribute from an object
58
58
  #
59
59
  # GET /examples/1/edit
60
60
  #
61
61
  def edit
62
62
  @object = @Klass.find(params[:id])
63
- @field = params[:field]
63
+ @attribute = params[:attribute]
64
64
  @form_element = params[:form_element]
65
- @values = params[:values]
66
65
  @sub_id = params[:sub_id]
67
66
  @update_span = params[:update]
68
67
  respond_to do |format|
@@ -79,69 +78,65 @@ class InlineFormsController < ApplicationController
79
78
  def create
80
79
  object = @Klass.new
81
80
  @update_span = params[:update]
82
- attributes = object.respond_to?(:inline_forms_field_list) ? object.inline_forms_field_list : [ '', :name, :text ] # sensible default
83
- attributes = [ attributes ] if not attributes[0].is_a?(Array) # make sure we have an array of arrays
84
- attributes.each do | name, attribute, form_element, values |
85
- send("#{form_element.to_s}_update", object, attribute, values)
81
+ attributes = object.inline_forms_attribute_list
82
+ attributes.each do | name, attribute, form_element |
83
+ send("#{form_element.to_s}_update", object, attribute)
86
84
  end
87
85
  object.save
88
- @objects = @Klass.all
86
+ @objects = @Klass.paginate :page => params[:page], :order => 'created_at DESC'
89
87
  respond_to do |format|
90
88
  # found this here: http://www.ruby-forum.com/topic/211467
91
89
  format.js { render(:update) {|page| page.replace_html @update_span, :partial => 'inline_forms/index'}
92
90
  }
93
91
  end
94
92
  end
95
- # :update updates a specific field from an object.
93
+ # :update updates a specific attribute from an object.
96
94
  #
97
95
  # PUT /examples/1
98
96
  #
99
97
  def update
100
98
  @object = @Klass.find(params[:id])
101
- @field = params[:field]
99
+ @attribute = params[:attribute]
102
100
  @form_element = params[:form_element]
103
- @values = params[:values]
104
101
  @sub_id = params[:sub_id]
105
102
  @update_span = params[:update]
106
- #@values = params[@Klass.to_s.downcase].is_a?(Array) ? params[@Klass.to_s.downcase][@field.to_sym] : nil
107
- send("#{@form_element.to_s}_update", @object, @field, @values)
103
+ send("#{@form_element.to_s}_update", @object, @attribute)
108
104
  @object.save
109
105
  respond_to do |format|
110
106
  # found this here: http://www.ruby-forum.com/topic/211467
111
- format.js { render(:update) {|page| page.replace_html @update_span, :inline => '<%= send("#{@form_element.to_s}_show", @object, @field, @values) %>' }
107
+ format.js { render(:update) {|page| page.replace_html @update_span, :inline => '<%= send("#{@form_element.to_s}_show", @object, @attribute) %>' }
112
108
  }
113
109
  end
114
110
  end
115
111
 
116
- # :show shows one field (attribute) from a record (object). It inludes the link to 'edit'
112
+ # :show shows one attribute (attribute) from a record (object). It inludes the link to 'edit'
117
113
  #
118
- # GET /examples/1?field=name&form_element=text
114
+ # GET /examples/1?attribute=name&form_element=text
119
115
  #
120
116
 
121
117
  def show
122
118
  @object = @Klass.find(params[:id])
123
- @field = params[:field]
119
+ @attribute = params[:attribute]
124
120
  @form_element = params[:form_element]
125
121
  if @form_element == "associated"
126
122
  @sub_id = params[:sub_id]
127
123
  if @sub_id.to_i > 0
128
- @associated_record_id = @object.send(@field.to_s.singularize + "_ids").index(@sub_id.to_i)
129
- @associated_record = @object.send(@field)[@associated_record_id]
124
+ @associated_record_id = @object.send(@attribute.to_s.singularize + "_ids").index(@sub_id.to_i)
125
+ @associated_record = @object.send(@attribute)[@associated_record_id]
130
126
  end
131
127
  end
132
128
  @update_span = params[:update]
133
- if @field.nil?
129
+ if @attribute.nil?
134
130
  respond_to do |format|
135
- @attributes = @object.respond_to?(:inline_forms_field_list) ? @object.inline_forms_field_list : [ :name, 'name', 'text_field' ]
131
+ @attributes = @object.inline_forms_attribute_list
136
132
  # found this here: http://www.ruby-forum.com/topic/211467
137
- format.js { render(:update) {|page| page.replace_html @update_span, :inline => '<%= send( "inline_forms_show_record", @object, @attributes) %>' }
133
+ format.js { render(:update) {|page| page.replace_html @update_span, :inline => '<%= send( "inline_forms_show_record", @object) %>' }
138
134
  }
139
135
  end
140
136
  else
141
- @values = params[:values]
142
137
  respond_to do |format|
143
138
  # found this here: http://www.ruby-forum.com/topic/211467
144
- format.js { render(:update) {|page| page.replace_html @update_span, :inline => '<%= send("#{@form_element}_show", @object, @field, @values) %>' }
139
+ format.js { render(:update) {|page| page.replace_html @update_span, :inline => '<%= send("#{@form_element}_show", @object, @attribute) %>' }
145
140
  }
146
141
  end
147
142
  end
@@ -1,92 +1,94 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:associated]=:references
3
- # associated
4
- def associated_show(object, attribute, values)
5
- #show a list of records
6
- out = ""
7
- if @sub_id && @sub_id.to_i > 0
8
- # if it's not a new record (sub_id > 0) then just update the list-element
9
- out << '<li>'
10
- out << link_to( @associated_record._presentation,
11
- send('edit_' + @Klass.to_s.underscore + '_path', object,
12
- :field => attribute,
13
- :sub_id => @sub_id,
14
- :form_element => this_method.reverse.sub(/.*_/,'').reverse,
15
- :values => values,
16
- :update => "field_#{attribute.to_s.singularize}_#{@sub_id.to_s}" ),
17
- :method => :get,
18
- :remote => true )
19
- out << '</li>'
20
- else
21
- # if it's a new record (sub_id == 0) then update the whole <ul> and redraw all list-elements
22
- out << "<ul class='associated #{attribute}' id='list_#{attribute}_#{object.id.to_s}'>" if @sub_id.nil?
23
- out << '<li class="associated_new">'
24
- out << link_to( 'new', send('edit_' + @Klass.to_s.underscore + '_path',
25
- object,
26
- :field => attribute,
27
- :sub_id => 0,
28
- :form_element => this_method.sub(/_[a-z]+$/,''),
29
- :values => values,
30
- :update => "list_#{attribute}_#{object.id.to_s}" ),
31
- :method => :get,
32
- :remote => true )
33
- out << '</li>'
34
- if not object.send(attribute.to_s.pluralize).empty?
35
- # if there are things to show, show them
36
- object.send(attribute.to_s.pluralize).each do |m|
37
- out << "<span id='field_#{attribute.to_s.singularize}_#{m.id.to_s}'>"
38
- out << '<li>'
39
- out << link_to( m._presentation, send('edit_' + @Klass.to_s.underscore + '_path',
40
- object,
41
- :field => attribute,
42
- :sub_id => m.id,
43
- :form_element => this_method.sub(/_[a-z]+$/,''),
44
- :values => values,
45
- :update => "field_#{attribute.to_s.singularize}_#{m.id.to_s}" ),
46
- :method => :get,
47
- :remote => true )
48
- out << '</li>'
49
- out << '</span>'
50
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:associated]=:references
2
+
3
+ # associated
4
+ def associated_show(object, attribute)
5
+ #show a list of records
6
+ out = ""
7
+ if @sub_id && @sub_id.to_i > 0
8
+ # if it's not a new record (sub_id > 0) then just update the list-element
9
+ out << '<li>'
10
+ out << link_to( @associated_record._presentation,
11
+ send('edit_' + @Klass.to_s.underscore + '_path', object,
12
+ :attribute => attribute,
13
+ :sub_id => @sub_id,
14
+ :form_element => this_method.reverse.sub(/.*_/,'').reverse,
15
+ :values => values,
16
+ :update => "attribute_#{attribute.to_s.singularize}_#{@sub_id.to_s}" ),
17
+ :method => :get,
18
+ :remote => true )
19
+ out << '</li>'
20
+ else
21
+ # if it's a new record (sub_id == 0) then update the whole <ul> and redraw all list-elements
22
+ out << "<ul class='associated #{attribute}' id='list_#{attribute}_#{object.id.to_s}'>" if @sub_id.nil?
23
+ out << '<li class="associated_new">'
24
+ out << link_to( 'new', send('edit_' + @Klass.to_s.underscore + '_path',
25
+ object,
26
+ :attribute => attribute,
27
+ :sub_id => 0,
28
+ :form_element => this_method.sub(/_[a-z]+$/,''),
29
+ :values => values,
30
+ :update => "list_#{attribute}_#{object.id.to_s}" ),
31
+ :method => :get,
32
+ :remote => true )
33
+ out << '</li>'
34
+ if not object.send(attribute.to_s.pluralize).empty?
35
+ # if there are things to show, show them
36
+ object.send(attribute.to_s.pluralize).each do |m|
37
+ out << "<span id='attribute_#{attribute.to_s.singularize}_#{m.id.to_s}'>"
38
+ out << '<li>'
39
+ out << link_to( m._presentation, send('edit_' + @Klass.to_s.underscore + '_path',
40
+ object,
41
+ :attribute => attribute,
42
+ :sub_id => m.id,
43
+ :form_element => this_method.sub(/_[a-z]+$/,''),
44
+ :values => values,
45
+ :update => "attribute_#{attribute.to_s.singularize}_#{m.id.to_s}" ),
46
+ :method => :get,
47
+ :remote => true )
48
+ out << '</li>'
49
+ out << '</span>'
51
50
  end
52
- # add a 'new' link for creating a new record
53
- out << '</ul>' if @sub_id.nil?
54
51
  end
55
- raw(out)
52
+ # add a 'new' link for creating a new record
53
+ out << '</ul>' if @sub_id.nil?
56
54
  end
57
- def associated_edit(object, attribute, values)
58
- # @sub_id is the id of the associated record
59
- if @sub_id.to_i > 0
60
- # only if @sub_id > 0, means we have a associated record
61
- @associated_record_id = object.send(attribute.to_s.singularize + "_ids").index(@sub_id.to_i)
62
- @associated_record = object.send(attribute)[@associated_record_id]
63
- @update_span = "field_#{attribute.to_s.singularize}_#{@sub_id.to_s}"
64
- else
65
- # but if @sub_id = 0, then we are dealing with a new associated record
66
- # in that case, we .new a record, and the update_span is the whole <ul>
67
- @associated_record = attribute.to_s.singularize.capitalize.constantize.new
68
- @update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
69
- end
70
- render :partial => "inline_forms/subform"
55
+ raw(out)
56
+ end
57
+
58
+ def associated_edit(object, attribute)
59
+ # @sub_id is the id of the associated record
60
+ if @sub_id.to_i > 0
61
+ # only if @sub_id > 0, means we have a associated record
62
+ @associated_record_id = object.send(attribute.to_s.singularize + "_ids").index(@sub_id.to_i)
63
+ @associated_record = object.send(attribute)[@associated_record_id]
64
+ @update_span = "attribute_#{attribute.to_s.singularize}_#{@sub_id.to_s}"
65
+ else
66
+ # but if @sub_id = 0, then we are dealing with a new associated record
67
+ # in that case, we .new a record, and the update_span is the whole <ul>
68
+ @associated_record = attribute.to_s.singularize.capitalize.constantize.new
69
+ @update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
71
70
  end
72
- def associated_update(object, attribute, values)
73
- return if object.id.nil?
74
- if @sub_id.to_i > 0
75
- # get the existing associated record
76
- @associated_record_id = object.send(attribute.to_s.singularize + "_ids").index(@sub_id.to_i)
77
- @associated_record = object.send(attribute)[@associated_record_id]
78
- @update_span = "field_" + attribute.to_s.singularize + '_' + @sub_id.to_s
79
- else
80
- # create a new associated record
81
- @associated_record = object.send(attribute.to_sym).new
82
- @update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
83
- end
84
- # process the sub_form fields (attributes). These are declared in the model!
85
- @associated_record.inline_forms_field_list.each do | @subform_description, @subform_field, @subform_element |
86
- # have no fear
87
- send("#{@subform_element}_update", @associated_record, @subform_field, nil)
88
- end
89
- @associated_record.save
71
+ render :partial => "inline_forms/subform"
72
+ end
73
+
74
+ def associated_update(object, attribute)
75
+ return if object.id.nil?
76
+ if @sub_id.to_i > 0
77
+ # get the existing associated record
78
+ @associated_record_id = object.send(attribute.to_s.singularize + "_ids").index(@sub_id.to_i)
79
+ @associated_record = object.send(attribute)[@associated_record_id]
80
+ @update_span = "attribute_" + attribute.to_s.singularize + '_' + @sub_id.to_s
81
+ else
82
+ # create a new associated record
83
+ @associated_record = object.send(attribute.to_sym).new
84
+ @update_span = 'list_' + attribute.to_s + '_' + object.id.to_s
90
85
  end
86
+ # process the sub_form attributes (attributes). These are declared in the model!
87
+ @associated_record.inline_forms_attribute_list.each do | @subform_description, @subform_attribute, @subform_element |
88
+ # have no fear
89
+ send("#{@subform_element}_update", @associated_record, @subform_attribute)
90
+ end
91
+ @associated_record.save
91
92
  end
92
93
 
94
+
@@ -1,16 +1,15 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:check_box]=:boolean
3
- # boolean, bit unaptly named check_box
4
- def check_box_show(object, attribute, values)
5
- values ||= { 'false' => 'no', 'true' => 'yes' }
6
- link_to_inline_edit object, attribute, values[object.send(attribute).to_s], values
7
- end
8
- def check_box_edit(object, attribute, values)
9
- values ||= { 'false' => 'no', 'true' => 'yes' }
10
- collection_select( object.class.to_s.downcase, attribute, values, 'first', 'last', :selected => object.send(attribute).to_s)
11
- end
12
- def check_box_update(object, attribute, values)
13
- object[attribute.to_s.to_sym] = params[object.class.to_s.downcase.to_sym][attribute.to_s.to_sym]
14
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:check_box]=:boolean
2
+ # boolean, bit unaptly named check_box
3
+ def check_box_show(object, attribute)
4
+ values ||= { 'false' => 'no', 'true' => 'yes' }
5
+ link_to_inline_edit object, attribute, values[object.send(attribute).to_s]
6
+ end
7
+
8
+ def check_box_edit(object, attribute)
9
+ check_box_tag attribute.to_s, 1, object.send(attribute)
10
+ end
11
+
12
+ def check_box_update(object, attribute)
13
+ object[attribute.to_s.to_sym] = params[attribute.to_s.to_sym].nil? ? 0 : 1
15
14
  end
16
15
 
@@ -1,36 +1,37 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:checklist]=:references
3
- # checklist
4
- def checklist_show(object, attribute, values)
5
- out = '<ul class="checklist">'
6
- out << link_to_inline_edit(object, attribute, nil, nil) if object.send(attribute).empty?
7
- object.send(attribute).sort.each do | item |
8
- out << '<li>'
9
- out << link_to_inline_edit(object, attribute, item.title, nil)
10
- out << '</li>'
11
- end
12
- out << '</ul>'
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:checklist]=:references
2
+
3
+ # checklist
4
+ def checklist_show(object, attribute)
5
+ out = '<ul class="checklist">'
6
+ out << link_to_inline_edit(object, attribute) if object.send(attribute).empty?
7
+ object.send(attribute).sort.each do | item |
8
+ out << '<li>'
9
+ out << link_to_inline_edit(object, attribute, item.title)
10
+ out << '</li>'
13
11
  end
14
- def checklist_edit(object, attribute, values)
15
- object.send(attribute).build if object.send(attribute).empty?
16
- values = object.send(attribute).first.class.name.constantize.find(:all) # TODO bring order
17
- out = '<div class="edit_form_checklist">'
18
- out << '<ul>'
19
- values.each do | item |
20
- out << '<li>'
21
- out << check_box_tag( attribute + '[' + item.id.to_s + ']', 'yes', object.send(attribute.singularize + "_ids").include?(item.id) )
22
- out << '<div class="edit_form_checklist_text">'
23
- out << h(item.title)
24
- out << '</div>'
25
- out << '<div style="clear: both;"></div>'
26
- out << '</li>'
27
- end
28
- out << '</ul>'
12
+ out << '</ul>'
13
+ end
14
+
15
+ def checklist_edit(object, attribute)
16
+ object.send(attribute).build if object.send(attribute).empty?
17
+ values = object.send(attribute).first.class.name.constantize.find(:all) # TODO bring order
18
+ out = '<div class="edit_form_checklist">'
19
+ out << '<ul>'
20
+ values.each do | item |
21
+ out << '<li>'
22
+ out << check_box_tag( attribute + '[' + item.id.to_s + ']', 'yes', object.send(attribute.singularize + "_ids").include?(item.id) )
23
+ out << '<div class="edit_form_checklist_text">'
24
+ out << h(item.title)
29
25
  out << '</div>'
26
+ out << '<div style="clear: both;"></div>'
27
+ out << '</li>'
30
28
  end
31
- def checklist_update(object, attribute, values)
32
- params[attribute] ||= {}
33
- object.send(attribute.singularize + '_ids=', params[attribute].keys)
34
- end
29
+ out << '</ul>'
30
+ out << '</div>'
31
+ end
32
+
33
+ def checklist_update(object, attribute)
34
+ params[attribute] ||= {}
35
+ object.send(attribute.singularize + '_ids=', params[attribute].keys)
35
36
  end
36
37
 
@@ -1,20 +1,20 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:date_select]=:date
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:date_select]=:date
3
2
 
4
- # date
5
- def date_select_show(object, attribute, values)
6
- link_to_inline_edit object, attribute, object.send(attribute), nil
7
- end
8
- def date_select_edit(object, attribute, values)
9
- out = text_field_tag attribute, object[attribute]
10
- out << '<SCRIPT>'.html_safe
11
- out << "$(function() { ".html_safe
12
- out << '$("#'.html_safe + attribute.to_s.html_safe + '").datepicker();'.html_safe
13
- out << '});'.html_safe
14
- out << '</SCRIPT>'.html_safe
15
- return out
16
- end
17
- def date_select_update(object, attribute, values)
18
- object[attribute.to_sym] = params[attribute.to_sym]
19
- end
3
+ # date
4
+ def date_select_show(object, attribute)
5
+ link_to_inline_edit object, attribute, object.send(attribute)
6
+ end
7
+
8
+ def date_select_edit(object, attribute)
9
+ out = text_field_tag attribute, object[attribute]
10
+ out << '<SCRIPT>'.html_safe
11
+ out << "$(function() { ".html_safe
12
+ out << '$("#'.html_safe + attribute.to_s.html_safe + '").datepicker();'.html_safe
13
+ out << '});'.html_safe
14
+ out << '</SCRIPT>'.html_safe
15
+ return out
16
+ end
17
+
18
+ def date_select_update(object, attribute)
19
+ object[attribute.to_sym] = params[attribute.to_sym]
20
20
  end