inline_forms 0.7.4 → 0.8.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,19 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:dropdown]=:belongs_to
3
- # dropdown
4
- def dropdown_show(object, attribute, values)
5
- attribute_value = object.send(attribute)._presentation rescue nil
6
- link_to_inline_edit object, attribute, attribute_value, nil
7
- end
8
- def dropdown_edit(object, attribute, values)
9
- object.send('build_' + attribute.to_s) unless object.send(attribute)
10
- values = object.send(attribute).class.name.constantize.find(:all) # TODO bring order!
11
- # 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!
12
- collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_s.foreign_key.to_sym, values, 'id', '_presentation', :selected => object.send(attribute).id)
13
- end
14
- def dropdown_update(object, attribute, values)
15
- object[attribute.to_s.foreign_key.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_s.foreign_key.to_sym]
16
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:dropdown]=:belongs_to
2
+
3
+ # dropdown
4
+ def dropdown_show(object, attribute)
5
+ attribute_value = object.send(attribute)._presentation rescue nil
6
+ link_to_inline_edit object, attribute, attribute_value
7
+ end
8
+
9
+ def dropdown_edit(object, attribute)
10
+ object.send('build_' + attribute.to_s) unless object.send(attribute)
11
+ values = object.send(attribute).class.name.constantize.find(:all) # TODO bring order!
12
+ # 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!
13
+ collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_s.foreign_key.to_sym, values, 'id', '_presentation', :selected => object.send(attribute).id)
14
+ end
15
+
16
+ def dropdown_update(object, attribute)
17
+ object[attribute.to_s.foreign_key.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_s.foreign_key.to_sym]
17
18
  end
18
19
 
@@ -1,26 +1,20 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:dropdown_with_integers]=:integer
3
- # dropdown_with_integers generates a dropdown menu
4
- # with the given list of integers as options
5
- #
6
- # values must be a Range or a one-dimensional array of Integers
7
- def dropdown_with_integers_show(object, attribute, values)
8
- unless values.is_a?(Hash)
9
- options = Array.new
10
- values.to_a.each_index do |i|
11
- options << [ i.to_s, values.to_a[i] ]
12
- end
13
- values = Hash[ *options.flatten ]
14
- end
15
- link_to_inline_edit object, attribute, values[object.send(attribute).to_s], values
16
- end
17
- def dropdown_with_integers_edit(object, attribute, values)
18
- # 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!
19
- values = values.sort {|a,b| a[1].to_i<=>b[1].to_i}
20
- collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
21
- end
22
- def dropdown_with_integers_update(object, attribute, values)
23
- object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
24
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:dropdown_with_integers]=:integer
2
+
3
+ # dropdown_with_integers generates a dropdown menu
4
+ # with the given list of integers as options
5
+ #
6
+ # values must be a Range or a one-dimensional array of Integers
7
+ def dropdown_with_integers_show(object, attribute)
8
+ values = attribute_values(object, attribute)
9
+ link_to_inline_edit object, attribute, values[object.send(attribute)][1]
10
+ end
11
+
12
+ def dropdown_with_integers_edit(object, attribute)
13
+ # 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!
14
+ values = attribute_values(object, attribute)
15
+ collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
25
16
  end
26
17
 
18
+ def dropdown_with_integers_update(object, attribute)
19
+ object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
20
+ end
@@ -1,23 +1,16 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:dropdown_with_values]=:integer
3
- # dropdown_with_values
4
- def dropdown_with_values_show(object, attribute, values)
5
- unless values.is_a?(Hash)
6
- options = Array.new
7
- values.to_a.each_index do |i|
8
- options << [ i.to_s, values.to_a[i] ]
9
- end
10
- values = Hash[ *options.flatten ]
11
- end
12
- link_to_inline_edit object, attribute, values[object.send(attribute).to_s], values
13
- end
14
- def dropdown_with_values_edit(object, attribute, values)
15
- # 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!
16
- values = values.sort {|a,b| a[1]<=>b[1]}
17
- collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
18
- end
19
- def dropdown_with_values_update(object, attribute, values)
20
- object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
21
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:dropdown_with_values]=:integer
2
+
3
+ # dropdown_with_values
4
+ def dropdown_with_values_show(object, attribute)
5
+ values = attribute_values(object, attribute)
6
+ link_to_inline_edit object, attribute, values[object.send(attribute).to_s]
7
+ end
8
+ def dropdown_with_values_edit(object, attribute)
9
+ # 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!
10
+ values = attribute_values(object, attribute)
11
+ collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
12
+ end
13
+ def dropdown_with_values_update(object, attribute)
14
+ object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
22
15
  end
23
16
 
@@ -1,17 +1,16 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:geo_code_curacao]=:string
3
- # geo_code_curacao
4
- def geo_code_curacao_show(object, attribute, values)
5
- attribute_value = object.send(attribute)._presentation rescue nil
6
- link_to_inline_edit object, attribute, attribute_value, nil
7
- end
8
- def geo_code_curacao_edit(object, attribute, values)
9
- text_field_with_auto_complete :geo_code_curacao, :street, :skip_style => true
10
- end
11
- def geo_code_curacao_update(object, attribute, values)
12
- # extract the geocode
13
- geo_code = params[attribute.to_sym][:street].scan(/\d\d\d\d\d\d/).to_s || nil
14
- object[attribute.to_sym] = GeoCodeCuracao.new(geo_code).valid? ? geo_code : nil
15
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:geo_code_curacao]=:string
2
+
3
+ # geo_code_curacao
4
+ def geo_code_curacao_show(object, attribute)
5
+ attribute_value = object.send(attribute)._presentation rescue nil
6
+ link_to_inline_edit object, attribute, attribute_value
7
+ end
8
+ def geo_code_curacao_edit(object, attribute)
9
+ text_field_with_auto_complete :geo_code_curacao, :street, :skip_style => true
10
+ end
11
+ def geo_code_curacao_update(object, attribute)
12
+ # extract the geocode
13
+ geo_code = params[attribute.to_sym][:street].scan(/\d\d\d\d\d\d/).to_s || nil
14
+ object[attribute.to_sym] = GeoCodeCuracao.new(geo_code).valid? ? geo_code : nil
16
15
  end
17
16
 
@@ -1,13 +1,14 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:image]=:image
3
- # image via paperclip
4
- def image_show(object, attribute, values)
5
- link_to_inline_image_edit object, attribute
6
- end
7
- def image_edit(object, attribute, values)
8
- file_field object.class.to_s.downcase, attribute
9
- end
10
- def image_update(object, attribute, values)
11
- object.send(attribute+'=',values)
12
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:image]=:image
2
+
3
+ # image via paperclip
4
+ def image_show(object, attribute)
5
+ link_to_inline_image_edit object, attribute
6
+ end
7
+
8
+ def image_edit(object, attribute)
9
+ file_attribute object.class.to_s.downcase, attribute
10
+ end
11
+
12
+ def image_update(object, attribute)
13
+ object.send(attribute+'=',values)
13
14
  end
@@ -1,26 +1,22 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:scale_with_integers]=:integer
3
- # scale_with_integers generates a sacle
4
- # with the given list of integers as options
5
- #
6
- # values must be a Range or a one-dimensional array of Integers
7
- def scale_with_integers_show(object, attribute, values)
8
- unless values.is_a?(Hash)
9
- options = Array.new
10
- values.to_a.each_index do |i|
11
- options << [ i.to_s, values.to_a[i] ]
12
- end
13
- values = Hash[ *options.flatten ]
14
- end
15
- link_to_inline_edit object, attribute, values[object.send(attribute).to_s], values
16
- end
17
- def scale_with_integers_edit(object, attribute, values)
18
- # 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!
19
- values = values.sort {|a,b| a[1].to_i<=>b[1].to_i}
20
- collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
21
- end
22
- def scale_with_integers_update(object, attribute, values)
23
- object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
24
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:scale_with_integers]=:integer
2
+
3
+ # scale_with_integers generates a scale
4
+ # with the given list of integers as options
5
+ #
6
+ # values must be a Range or a one-dimensional array of Integers
7
+ #
8
+ def scale_with_integers_show(object, attribute)
9
+ values = attribute_values(object, attribute)
10
+ link_to_inline_edit object, attribute, values[object.send(attribute).to_s]
11
+ end
12
+
13
+ def scale_with_integers_edit(object, attribute)
14
+ # 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!
15
+ values = attribute_values(object, attribute)
16
+ collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
17
+ end
18
+
19
+ def scale_with_integers_update(object, attribute)
20
+ object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
25
21
  end
26
22
 
@@ -0,0 +1,21 @@
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:scale_with_values]=:integer
2
+
3
+ # scale_with_values generates a scale
4
+ # with the given list of values as options
5
+ #
6
+ # values must be a hash { integer => string, ... } or an one-dimensional array of strings
7
+ def scale_with_values_show(object, attribute)
8
+ values = attribute_values(object, attribute)
9
+ link_to_inline_edit object, attribute, values[object.send(attribute)]
10
+ end
11
+
12
+ def scale_with_values_edit(object, attribute)
13
+ # 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!
14
+ values = attribute_values(object, attribute)
15
+ collection_select( ('_' + object.class.to_s.downcase).to_sym, attribute.to_sym, values, 'first', 'last', :selected => object.send(attribute))
16
+ end
17
+
18
+ def scale_with_values_update(object, attribute)
19
+ object[attribute.to_sym] = params[('_' + object.class.to_s.downcase).to_sym][attribute.to_sym]
20
+ end
21
+
@@ -1,13 +1,13 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:text_area]=:text
3
- # text_area
4
- def text_area_show(object, attribute, values)
5
- link_to_inline_edit object, attribute, object.send(attribute), nil
6
- end
7
- def text_area_edit(object, attribute, values)
8
- text_area_tag attribute, object[attribute], :class => 'field_text_area'
9
- end
10
- def text_area_update(object, attribute, values)
11
- object[attribute.to_sym] = params[attribute.to_sym]
12
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:text_area]=:text
2
+
3
+ def text_area_show(object, attribute)
4
+ link_to_inline_edit object, attribute, object.send(attribute)
5
+ end
6
+
7
+ def text_area_edit(object, attribute)
8
+ text_area_tag attribute, object[attribute], :class => 'attribute_text_area'
9
+ end
10
+
11
+ def text_area_update(object, attribute)
12
+ object[attribute.to_sym] = params[attribute.to_sym]
13
13
  end
@@ -1,14 +1,14 @@
1
- module InlineFormsHelper
2
- InlineForms::SPECIAL_COLUMN_TYPES[:text_field]=:string
3
- # text
4
- def text_field_show(object, attribute, values)
5
- link_to_inline_edit object, attribute, object.send(attribute), nil
6
- end
7
- def text_field_edit(object, attribute, values)
8
- text_field_tag attribute, object[attribute], :class => 'input_text_field'
9
- end
10
- def text_field_update(object, attribute, values)
11
- object[attribute.to_sym] = params[attribute.to_sym]
12
- end
1
+ InlineForms::SPECIAL_COLUMN_TYPES[:text_field]=:string
2
+
3
+ def text_field_show(object, attribute)
4
+ link_to_inline_edit object, attribute, object.send(attribute)
5
+ end
6
+
7
+ def text_field_edit(object, attribute)
8
+ text_field_tag attribute, object[attribute], :class => 'input_text_field'
9
+ end
10
+
11
+ def text_field_update(object, attribute)
12
+ object[attribute.to_sym] = params[attribute.to_sym]
13
13
  end
14
14
 
@@ -1,24 +1,31 @@
1
- INLINE_FORMS_PATH = File.dirname(__FILE__) + "/form_elements/"
1
+ module InlineFormsHelper
2
2
 
3
- Dir[INLINE_FORMS_PATH + "*.rb"].each do |form_element|
4
- require form_element
5
- end
3
+ # load form elements. Each element goes into a separate file
4
+ # and defines a _show, _edit and _update method.
5
+ #
6
+ INLINE_FORMS_PATH = File.dirname(__FILE__) + "/form_elements/"
7
+ Dir[INLINE_FORMS_PATH + "*.rb"].each do |form_element|
8
+ require form_element
9
+ end
6
10
 
7
- module InlineFormsHelper
8
- def inline_forms_show_record(object, attributes)
9
- attributes = [ attributes ] unless attributes[0].is_a?(Array) # make sure we have an array of arrays
11
+ # default inline forms attribute list, for models that don't have an inline_attribute_attribute_list defined.
12
+ INLINE_FORMS_DEFAULT_ATTRIBUTE_LIST = { :name => [ 'name', :text ] }
13
+
14
+ # show a record by iterating through its attribute list
15
+ def inline_forms_show_record(object, attributes=nil)
16
+ attributes ||= object.inline_forms_attribute_list
10
17
  out = String.new
11
- attributes.each do | attribute, name, form_element, values |
12
- css_class_id = "field_#{attribute.to_s}_#{object.id}"
18
+ attributes.each do | attribute, name, form_element |
19
+ css_class_id = "attribute_#{attribute}_#{object.id}"
13
20
  name_cell = content_tag :td, :valign=>'top' do
14
- content_tag :div, :class=> "field_name field_#{attribute.to_s} form_element_#{form_element.to_s}" do
21
+ content_tag :div, :class=> "attribute_name attribute_#{attribute} form_element_#{form_element}" do
15
22
  h(name)
16
23
  end
17
24
  end
18
25
  value_cell = content_tag :td, :valign=>'top' do
19
- content_tag :div, :class=> "field_value field_#{attribute.to_s} form_element_#{form_element.to_s}" do
26
+ content_tag :div, :class=> "attribute_value attribute_#{attribute} form_element_#{form_element}" do
20
27
  content_tag :span, :id => css_class_id do
21
- send("#{form_element.to_s}_show", object, attribute, values)
28
+ send("#{form_element}_show", object, attribute)
22
29
  end
23
30
  end
24
31
  end
@@ -29,22 +36,25 @@ module InlineFormsHelper
29
36
  return content_tag :table, raw(out), :cellspacing => 0, :cellpadding => 0
30
37
  end
31
38
 
32
- def inline_form_display_new(object, attributes)
33
- attributes = [ attributes ] if not attributes[0].is_a?(Array) # make sure we have an array of arrays
34
- out = String.new #ugly as hell but that's how content_tag works...
35
- attributes.each do | attribute, name, form_element, values |
36
- #css_class_id = form_element == :associated ? "subform_#{attribute.to_s}_#{object.id}" : "field_#{attribute.to_s}_#{object.id}"
39
+ # show the form for a new record
40
+ #
41
+ # associated records are NOT shown!
42
+ #
43
+ def inline_forms_new_record(object, attributes=nil)
44
+ attributes ||= object.inline_forms_attribute_list
45
+ out = String.new
46
+ attributes.each do | attribute, name, form_element |
37
47
  if not form_element.to_sym == :associated
38
- css_class_id = "field_#{attribute.to_s}_#{object.id}"
48
+ css_class_id = "attribute_#{attribute}_#{object.id}"
39
49
  name_cell = content_tag :td, :valign=>'top' do
40
- content_tag :div, :class=> "field_name field_#{attribute.to_s} form_element_#{form_element.to_s}" do
50
+ content_tag :div, :class=> "attribute_name attribute_#{attribute} form_element_#{form_element}" do
41
51
  h(name)
42
52
  end
43
53
  end
44
54
  value_cell = content_tag :td, :valign=>'top' do
45
- content_tag :div, :class=> "field_value field_#{attribute.to_s} form_element_#{form_element.to_s}" do
55
+ content_tag :div, :class=> "attribute_value attribute_#{attribute} form_element_#{form_element}" do
46
56
  content_tag :span, :id => css_class_id do
47
- send("#{form_element.to_s}_edit", object, attribute, values)
57
+ send("#{form_element}_edit", object, attribute)
48
58
  end
49
59
  end
50
60
  end
@@ -53,50 +63,76 @@ module InlineFormsHelper
53
63
  end
54
64
  return content_tag :table, raw(out), :cellspacing => 0, :cellpadding => 0
55
65
  end
66
+
56
67
  # display a list of objects
57
- def inline_form_display_list(objects, tag=:li)
58
- t = ''
68
+ def inline_forms_list(objects, tag=:li)
69
+ t = String.new
59
70
  objects.each do |object|
60
71
  css_class_id = @Klass.to_s.underscore + '_' + object.id.to_s
61
72
  t += content_tag tag, :id => css_class_id do
62
- #inline_forms_show_record object, object.respond_to?(:inline_forms_field_list) ? object.inline_forms_field_list : [ :name, 'name', 'text_field' ]
63
- link_to( h( object._presentation ), send( @Klass.to_s.underscore + '_path', object, :update => css_class_id), :remote => true )
73
+ link_to h(object._presentation),
74
+ send( @Klass.to_s.underscore + '_path', object, :update => css_class_id),
75
+ :remote => true
64
76
  end
65
77
  end
66
78
  return raw(t)
67
79
  end
80
+
68
81
  # link for new item
69
- def inline_form_new_record(attribute, form_element, text='new', update_span='inline_form_list')
70
- link_to text, send('new_' + @Klass.to_s.underscore + '_path', :update => update_span), :remote => true
82
+ def inline_forms_new_record_link(text='new', update_span='inline_forms_list')
83
+ link_to text,
84
+ send('new_' + @Klass.to_s.underscore + '_path', :update => update_span),
85
+ :remote => true
71
86
  end
72
-
73
-
74
87
 
75
88
  private
76
89
 
77
90
  # link_to_inline_edit
78
- def link_to_inline_edit(object, attribute, attribute_value, values)
91
+ def link_to_inline_edit(object, attribute, attribute_value)
79
92
  attribute_value = h(attribute_value)
80
93
  spaces = attribute_value.length > 40 ? 0 : 40 - attribute_value.length
81
94
  attribute_value << "&nbsp;".html_safe * spaces
82
- link_to raw(attribute_value), send('edit_' + @Klass.to_s.underscore + '_path',
83
- object,
84
- :field => attribute.to_s,
85
- :form_element => calling_method.sub(/_[a-z]+$/,''),
86
- :values => values,
87
- :update => 'field_' + attribute.to_s + '_' + object.id.to_s ),
88
- :remote => true
95
+ link_to raw(attribute_value),
96
+ send( 'edit_' + @Klass.to_s.underscore + '_path',
97
+ object,
98
+ :attribute => attribute.to_s,
99
+ :form_element => calling_method.sub(/_[a-z]+$/,''),
100
+ :update => "attribute_#{attribute}_#{object.id}" ),
101
+ :remote => true
89
102
  end
90
103
 
104
+ # link to inline image edit
91
105
  def link_to_inline_image_edit(object, attribute)
92
- text= image_tag object.send(attribute.to_s).send('url', :thumb)
106
+ text= image_tag object.send(attribute).send('url', :thumb)
107
+ link_to text,
108
+ send('edit_' + @Klass.to_s.underscore + '_path',
109
+ object,
110
+ :attribute => attribute.to_s,
111
+ :form_element => calling_method.sub(/_[a-z]+$/,''),
112
+ :update => "attribute_#{attribute}_#{object.id}" ),
113
+ :remote => true
114
+ end
93
115
 
94
- link_to text, send('edit_' + @Klass.to_s.underscore + '_path',
95
- object,
96
- :field => attribute.to_s,
97
- :form_element => calling_method.sub(/_[a-z]+$/,''),
98
- :update => 'field_' + attribute.to_s + '_' + object.id.to_s ),
99
- :remote => true
116
+ # get the values for an attribute
117
+ #
118
+ # values should be a Hash { integer => string, ... }
119
+ #
120
+ # or a one-dimensional array of strings
121
+ #
122
+ # or a Range
123
+ #
124
+ def attribute_values(object, attribute)
125
+ values = object.inline_forms_attribute_list.assoc(attribute.to_sym)[3]
126
+ raise "No Values defined in #{@Klass}, #{attribute}" if values.nil?
127
+ unless values.is_a?(Hash)
128
+ temp = Array.new
129
+ values.to_a.each_index do |i|
130
+ temp << [ i, values.to_a[i] ]
131
+ end
132
+ values = temp.sort {|a,b| a[1]<=>b[1]}
133
+ end
134
+ logger.info values.inspect
135
+ values
100
136
  end
101
137
 
102
138
  end