king_views 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +84 -0
  3. data/Rakefile +31 -0
  4. data/VERSION +1 -0
  5. data/init.rb +3 -0
  6. data/king_form/MIT-LICENSE +20 -0
  7. data/king_form/README.rdoc +64 -0
  8. data/king_form/Rakefile +23 -0
  9. data/king_form/init.rb +1 -0
  10. data/king_form/lib/king_form.rb +19 -0
  11. data/king_form/lib/king_form/builder/base.rb +238 -0
  12. data/king_form/lib/king_form/builder/definition_list.rb +119 -0
  13. data/king_form/lib/king_form/builder/form_fields.rb +333 -0
  14. data/king_form/lib/king_form/builder/form_fields_overrides.rb +146 -0
  15. data/king_form/lib/king_form/builder/labeled.rb +116 -0
  16. data/king_form/lib/king_form/helper.rb +97 -0
  17. data/king_form/lib/king_form/nested_form_helper.rb +61 -0
  18. data/king_form/lib/king_form/overrides.rb +25 -0
  19. data/king_form/tasks/king_forms_tasks.rake +4 -0
  20. data/king_form/test/king_forms_test.rb +8 -0
  21. data/king_form/test/test_helper.rb +3 -0
  22. data/king_format/MIT-LICENSE +20 -0
  23. data/king_format/README.rdoc +20 -0
  24. data/king_format/Rakefile +23 -0
  25. data/king_format/init.rb +1 -0
  26. data/king_format/lib/helpers/date_helper.rb +25 -0
  27. data/king_format/lib/helpers/formatting_helper.rb +108 -0
  28. data/king_format/lib/helpers/money_helper.rb +63 -0
  29. data/king_format/lib/king_format.rb +14 -0
  30. data/king_format/lib/model_mixins/has_date_fields.rb +42 -0
  31. data/king_format/lib/model_mixins/has_money_fields.rb +42 -0
  32. data/king_format/lib/model_mixins/has_percent_fields.rb +34 -0
  33. data/king_format/tasks/king_format_tasks.rake +4 -0
  34. data/king_format/test/king_format_test.rb +8 -0
  35. data/king_format/test/test_helper.rb +3 -0
  36. data/king_list/MIT-LICENSE +20 -0
  37. data/king_list/README.rdoc +21 -0
  38. data/king_list/Rakefile +23 -0
  39. data/king_list/init.rb +1 -0
  40. data/king_list/lib/king_list.rb +18 -0
  41. data/king_list/lib/king_list/app_helper.rb +30 -0
  42. data/king_list/lib/king_list/builder/show.rb +71 -0
  43. data/king_list/lib/king_list/builder/table.rb +166 -0
  44. data/king_list/lib/king_list/list_helper.rb +329 -0
  45. data/king_list/lib/king_list/overrides.rb +6 -0
  46. data/king_list/tasks/king_list_tasks.rake +4 -0
  47. data/king_list/test/king_list_test.rb +8 -0
  48. data/king_list/test/test_helper.rb +3 -0
  49. data/king_views.gemspec +85 -0
  50. metadata +110 -0
@@ -0,0 +1,119 @@
1
+ module KingForm
2
+ module Builder
3
+ # Subclass of the KingFormBuilder for forms/fields wrapped inside a Definition List dl>dt,dd.
4
+ # This class just overides the nessessary html wrapping function of the KingFormBuilder.
5
+ #
6
+ # ====Example haml
7
+ # - dl_form_for(current_object) do |f|
8
+ # = f.section _('legend.user.details') do
9
+ # = f.text :first_name
10
+ # = f.text :last_name
11
+ #
12
+ # => # <fieldset>
13
+ # <legend>User Details</legend>
14
+ # <dl>
15
+ # <dt>Firstname</dt>
16
+ # <dd><input type='text' value='Otto'/></dd>
17
+ # <dt>Lastname</dt>
18
+ # <dd><input type='text' value='Bismark'/></dd>
19
+ # </dl>
20
+ # </fieldset>
21
+ #
22
+ class DefinitionList < KingForm::Builder::Base
23
+
24
+ # Create a section(fieldset) within a form
25
+ # A section is a group of related object information with name/value pairs,
26
+ # like all dates of an object or the users name fields(last/first/title/nick).
27
+ #
28
+ # A section html consists of a fieldset > legend > dl > dt > dd
29
+ # The dt holds the title/description (DefinitionType) of the current field
30
+ # The dd holds the value.
31
+ # This wrapup is preferred over ul/li or other listing types because of
32
+ # the semantic meaning of the html
33
+ #
34
+ #===Example haml
35
+ # = f.section _('legend.user.details') do
36
+ # = f.text :first_name
37
+ # = f.text :last_name
38
+ #
39
+ # => # <fieldset>
40
+ # <legend>User Details</legend>
41
+ # <dl>
42
+ # <dt>Firstname</dt>
43
+ # <dd><input type='text' value='Otto'/></dd>
44
+ # <dt>Lastname</dt>
45
+ # <dd><input type='text' value='Bismark'/></dd>
46
+ # </dl>
47
+ # </fieldset>
48
+ #
49
+ # = f.section 'User', :class=>'settings', :dl=>{:class=>'left'}
50
+ #
51
+ # => # <fieldset class='settings'>
52
+ # <legend>User</legend>
53
+ # <dl class='left'>
54
+ def section(title = nil, options = {}, &block)
55
+ raise ArgumentError if title && !title.is_a?(String)
56
+ dl_options = options[:dl] || {}
57
+ @template.haml_tag :fieldset, options do
58
+ @template.haml_tag :legend, title unless title.blank?
59
+ @template.haml_tag :dl, dl_options do
60
+ @template.haml_concat( @template.capture_haml(&block) )
61
+ end
62
+ end
63
+ end
64
+
65
+ # add titles to Input-Tag and embed/wrap in dt/dd
66
+ #
67
+ # ==== Parameter
68
+ # fieldname_or_title<Symbol, String>:: The fieldname to be used as the title in dt
69
+ # tags<String>:: a bunch of html/haml tags
70
+ # options<Hash{Symbol=>String}>: Hash with following keys
71
+ # ==== Options:
72
+ # :dt => options hash for dt
73
+ # :dd => options hash for dd
74
+ def tag_wrapper(fieldname_or_title, tags, options = {})
75
+ if @config[:bundle] # called from "bundle" => dt/dd-wrapping is made outside
76
+ @bundle_counter += 1
77
+ tags.to_s
78
+ elsif @config[:table] # called from "table" => build a table cell (td)
79
+ # Only in first row: Build column header
80
+ if @config[:row_number] == 1
81
+ @config[:column_header].push :title => build_title(fieldname_or_title),
82
+ :options => { :align => options[:align] || 'left' }
83
+ end
84
+ @template.capture_haml do #MUST return as string
85
+ @template.haml_tag(:td, tags.to_s, options)
86
+ end
87
+ else #wrap or no wrap
88
+ @no_wrap ? tags : dt_tag(fieldname_or_title, options[:dt]) + dd_tag(tags.to_s, options[:dd])
89
+ end
90
+ end
91
+
92
+ # Show multiple inputs in one line (dd tag)
93
+ # === Example haml
94
+ # - f.bundle _('Gender and Title') do
95
+ # = f.selection :gender
96
+ # = f.text :title, :medium
97
+ #
98
+ def bundle(title = nil, options = {}, &block)
99
+ @config[:bundle] = true
100
+ @bundle_counter = 0
101
+ tags = @template.capture(&block)
102
+ @config[:bundle] = false
103
+ @template.concat( @config[:table] ? tag_wrapper(title, tags) : tag_wrapper(title, tags, :dt => options, :dd => { :class => "elements_#{@bundle_counter}" }) )
104
+ end
105
+
106
+ # The definition type dt tag
107
+ def dt_tag(fieldname_or_title, options = {})
108
+ fieldname_or_title.blank? ? "" : content_tag(:dt, build_title(fieldname_or_title), options)
109
+ end
110
+
111
+ # Build dd-tag
112
+ # Parameter "tags" may be a string or an array of strings
113
+ def dd_tag(tags, options = {})
114
+ tags.blank? ? '' : content_tag(:dd, tags.to_s, options)
115
+ end
116
+
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,333 @@
1
+ module KingForm
2
+ module Builder
3
+ module FormFields
4
+ ##########################################################################
5
+ # Convinience Methods for simpler template tags
6
+ ##########################################################################
7
+
8
+ # Generate input tag with title for text editing
9
+ # ==== Example
10
+ # 1) text :name
11
+ # 2) text :name, :title => 'Your Name:', :value => @the_name, :size=>30, .maxlength=>35
12
+ #
13
+ # ==== Parameter
14
+ # fieldname<Symbol, String>:: The field name of the current object
15
+ # options<Hash{Symbol=>Sting}:: Options to customize the output
16
+ # ==== Options
17
+ # :title => the title for the field
18
+ # :maxlength => the maxlength for the field, defaults to the columns limit
19
+ # :size => the size for the field, defaults to 25 or the columns minimun length
20
+ # :value => the value if it differs from th the fields value
21
+ def text(fieldname, options={})
22
+ title = options.delete(:title) || build_title(fieldname)
23
+
24
+ if current_object.is_a?(ActiveRecord::Base)
25
+ if column = current_object.class.columns_hash[fieldname.to_s]
26
+ # Limit the length of input to the column capacity
27
+ options[:maxlength] ||= column.limit
28
+ # Limit the displayed width to 25 (or to the column capacity, if smaller)
29
+ options[:size] ||= [25, column.limit].min
30
+ end
31
+ tag_wrapper title, text_field(fieldname, options)
32
+ else
33
+ value = options.delete(:value) || current_value(fieldname)
34
+ tag_wrapper title, text_field_tag(fieldname, value, options)
35
+ end
36
+ end
37
+
38
+ # Generate hidden field tag
39
+ # ==== Example
40
+ # Default usage:
41
+ # - dl_form_for @email do |e|
42
+ # = e.hidden :some_token
43
+ # => <input type='hidden' value='value of email.some_token' name=email[some_token]..>
44
+ # Custom fieldname passed in as string:
45
+ # e.hidden 'email[attachment_ids][]', value =>' '
46
+ # => <input type='hidden' value=' ' name='email[attachment_ids][]'..>
47
+ # ==== Parameter
48
+ # fieldname<String>:: The fieldname and the value for it (on current object)
49
+ # fieldname<Symbol>:: If there is an AR:Base Object present the fieldname and
50
+ # the options are passed to hidden_field
51
+ # options<Hash>:: default options for hidden_field and hidden_fiel_tag plus:
52
+ # :value => the value to insert
53
+ def hidden(fieldname, options={})
54
+ if current_object.is_a?(ActiveRecord::Base) && fieldname.is_a?(Symbol)
55
+ hidden_field(fieldname, options)
56
+ else # not an AR object, or a custom field
57
+ value = options.delete(:value) || current_value(fieldname)
58
+ hidden_field_tag(fieldname, value, options)
59
+ end
60
+ end
61
+
62
+ # Generate password input tag with title
63
+ #
64
+ # ==== Example
65
+ # password :password
66
+ # password(:pasword, :title => 'What is your password?', :value => @the_password)
67
+ def password(fieldname, options={})
68
+ title = options.delete(:title) || build_title(fieldname)
69
+
70
+ if current_object.is_a?(ActiveRecord::Base)
71
+ tag_wrapper title, password_field(fieldname, options)
72
+ else
73
+ value = options.delete(:value) || current_value(fieldname)
74
+ tag_wrapper title, password_field_tag(fieldname, value, options)
75
+ end
76
+ end
77
+
78
+ # Generate textarea input tag with title for multiple line text editing
79
+ #
80
+ # ==== Parameters
81
+ # fieldname<Symbol>:: the name of the textarea
82
+ # options<Hash>:: options which will be passed on to textarea helper
83
+ # html_options<Hash>:: options which will be passed on to the wrapping element
84
+ #
85
+ # ==== Example
86
+ # 1) memo(:notes)
87
+ # 2) memo(:comment, :title => 'Comment', :value => @the_comment)
88
+ def memo(fieldname, options={}, html_options={})
89
+ title = options.delete(:title) || build_title(fieldname)
90
+
91
+ if current_object.is_a?(ActiveRecord::Base)
92
+ tag_wrapper title, text_area(fieldname, options), html_options
93
+ else
94
+ value = options.delete(:value) || current_value(fieldname)
95
+ tag_wrapper title, text_area_tag(fieldname, value, options),html_options
96
+ end
97
+ end
98
+
99
+ # Generate date select tags (day/month/year) with title
100
+ #
101
+ # ==== Example
102
+ # date :birthday, :title => 'Geburtstag'
103
+ def date(fieldname, options={}, html_options={})
104
+ title = options.delete(:title) || build_title(fieldname)
105
+ options[:include_blank] ||= true
106
+ css_class = options[:class] || ''
107
+ tag_wrapper title, date_select(fieldname, options, html_options), :dd => { :class => css_class + ' dates' }, :dt => {:class => css_class}
108
+ end
109
+
110
+ # Generate datetime select tags (day/month/year and hour/minute) with title
111
+ #
112
+ # Usage:
113
+ # datetime :starts_at, :title => 'Beginnt um'
114
+ def datetime(fieldname, options={})
115
+ title = options.delete(:title) || build_title(fieldname)
116
+ options[:include_blank] ||= true
117
+
118
+ tag_wrapper title, datetime_select(fieldname, options), :dd => { :class => 'dates' }
119
+ end
120
+
121
+ # Generate select tag with title for text editing
122
+ #
123
+ # === Example haml
124
+ # - dl_fields_for @user do |u|
125
+ # = u.selection :gender
126
+ # = u.selection :currency, { :choices => %w(EUR USW JPY) }, { :class => 'my-css-class' }
127
+ # = u.selection :status, :title => 'Invoice status', :choices => %w(draft published)
128
+ # = u.selection 'user[custom_field]', :title => 'Invoice status', :choices => %w(draft published)
129
+ #
130
+ # - dl_fields_for 'custom_obj' do |c|
131
+ # = c.selection :project_id, :choices => @projects, :title => 'Projects'
132
+ # = c.selection 'custom_obj[pills]', :choices => @red_pills, :title => "Choose your Pill", :info=>'Help text'
133
+ #
134
+ # ==== Parameter
135
+ # fieldname<String, Symbol>:: The name of the field. Used to build the translated title, find enum values.
136
+ # When passing a string, the field is not looked up on the current AR Object,
137
+ # use it to give it some custom name, which does not exist on the object
138
+ # options<Hash{Symbol=>String}>::options to configure the select behaviour
139
+ # html_options<Hash{Symbol=>String}>::options passed to the html options of the select
140
+ #
141
+ # ==== Options (options)
142
+ # :title<String>:: Field title, used in dt or label tag
143
+ # :choices<(Array[Strings],Array[Array[String,String]],Hash)>:: The choice for the select.
144
+ # :value<String>::The value of the select which will be used as selected unless selected is given. Defaults to the current objects.fieldname => value
145
+ # :selected<String>:: The selected value is taken from value
146
+ # :include_blank<Boolean>:: true/false (default is true)
147
+ # ==== Options (html_options)
148
+ # see select and select_tag in rails
149
+ def selection(fieldname, options={}, html_options={})
150
+ title = options.delete(:title) || build_title(fieldname)
151
+ choices = options.delete(:choices) || enum_values(fieldname) || []
152
+ value = options.delete(:value) || current_value(fieldname)
153
+ options[:include_blank] = true unless options.has_key?(:include_blank)
154
+ options[:selected] ||= value.is_a?(Array) ? value : value.to_s
155
+
156
+ # Got an AR object so full automatic contruction should work
157
+ if current_object.is_a?(ActiveRecord::Base) && fieldname.is_a?(Symbol)
158
+ # try to sort by key f.ex. when transl. enum_fields
159
+ choices = choices.to_a.sort_by{|k|k} if choices.is_a?(Hash)
160
+ tag_wrapper title, select(fieldname, choices, options, html_options)
161
+ else # a custom object
162
+ # got an array of sub-arrays[[key,val]] or an array of strings(key==val)
163
+ if choices.is_a?(Array) && (choices.first.is_a?(Array) || choices.first.is_a?(String))
164
+ # select_tag does not support :include_blank, so do it the manual way
165
+ choices.insert(0, '') if options.delete(:include_blank)
166
+ option_tags = @template.options_for_select(choices, options.delete(:selected))
167
+ # convert name to string before select_tag due to auto created info+id when it recieves a symbol
168
+ # This prevents one from passing an empty :info=>'', when using a custom object with symbol syntax
169
+ fld_name = fieldname.is_a?(String) ? fieldname : "#{@object_name}[#{fieldname}]"
170
+ tag_wrapper title, select_tag(fld_name, option_tags, options.merge(html_options))
171
+ elsif choices.is_a?(Hash)
172
+ # select_tag does not support :include_blank, so do it the manual way
173
+ choices[nil] = '' if options.delete(:include_blank)
174
+ option_tags = @template.options_for_select(choices, options.delete(:selected))
175
+ tag_wrapper title, select_tag(fieldname, option_tags, options.merge(html_options))
176
+ else #Choices of AR Objects, value is the Obj id, shown value is the object.to_s
177
+ # choices = choices.sort_by{|k, v|v.to_s} # sorty by object as string
178
+ choices = choices.sort_by{|k| k.to_s} # sorty by object as string
179
+ tag_wrapper title, collection_select(fieldname, choices, :id, :to_s, options, html_options)
180
+ end
181
+ end
182
+ end
183
+
184
+ # Generate select tag with options groups for a multiple collections of objects
185
+ #
186
+ # by default the :id of each object is taken as value and the :to_s method is
187
+ # used for options text.
188
+ #
189
+ # if an object_group is empty it will be omitted
190
+ #
191
+ # === Example haml
192
+ #
193
+ # f.selection_group :id, :labels => %w(defaults, own), :choices =>[@objects_1, @objects_2], :title =>'my custom title'
194
+ #
195
+ # f.selection_group :id, :title => 'Template',
196
+ # :choices => [ @pdf_templates, @pdf_default_templates],
197
+ # :labels=>[ 'User Templates', 'Default Templates']
198
+ #
199
+ #
200
+ # Available options keys:
201
+ # :title
202
+ # :choices => array of object groups, which are used for each option grouped,
203
+ # in their order of usage
204
+ # :labels => the label for each option group, in order of usage
205
+ # :selected
206
+ def selection_group(fieldname, options={})
207
+ labels = options.delete(:labels)
208
+ title = options.delete(:title) || build_title(fieldname)
209
+ choices = options.delete(:choices)
210
+ selected = options.delete(:selected)
211
+
212
+ # TODO: Uses option_groups_from_collection_for_select here!
213
+ select_options = ''
214
+ choices.each_with_index do |v,k| #build optiongroup for each given object group
215
+ unless v.empty?
216
+ select_options << "<optgroup label='#{labels[k]}'>"
217
+ select_options << @template.options_from_collection_for_select(v, :id, :to_s, selected) #still hardcoded
218
+ select_options << "</optgroup>"
219
+ end
220
+ end
221
+ tag_wrapper title, select_tag(fieldname, select_options, options)
222
+ end
223
+
224
+ def time_zone_selection(fieldname, options = {}, html_options = {})
225
+ tag_wrapper fieldname, time_zone_select("#{fieldname}", ActiveSupport::TimeZone.all, options, html_options)
226
+ end
227
+
228
+ def checkbox(fieldname, options={})
229
+ title = options.delete(:title) || build_title(fieldname)
230
+ if current_object.is_a?(ActiveRecord::Base) && fieldname.is_a?(Symbol)
231
+ tag_wrapper title, check_box(fieldname, options)
232
+ else # not an AR object, or a custom field
233
+ # value = # defaults to 1
234
+ tag_wrapper title, check_box_tag(fieldname, options.delete(:value), options.delete(:checked), options)
235
+ end
236
+ end
237
+
238
+ def radio(fieldname, tag_value, options={})
239
+ title = options.delete(:title) || build_title(fieldname)
240
+ tag_wrapper title, radio_button(fieldname, tag_value, options)
241
+ end
242
+
243
+ #file upload field
244
+ def file(fieldname, options={})
245
+ title = options.delete(:title) || build_title(fieldname)
246
+
247
+ if current_object.is_a?(ActiveRecord::Base) && fieldname.is_a?(Symbol)
248
+ tag_wrapper title, file_field(fieldname, options)
249
+ else
250
+ value = options.delete(:value) || current_value(fieldname)
251
+ tag_wrapper title, file_field_tag(fieldname, options)
252
+ end
253
+ end
254
+
255
+ # Create a submit button
256
+ # due to better formating the default button call is wrapped in a span.
257
+ #
258
+ # === Example (haml)
259
+ # - f.actions do
260
+ # = f.submit t('form.save')
261
+ #
262
+ # = f.submit "save", :span => {:class=>'custom class'}
263
+ #
264
+ # = f.submit 'Submit Me', :name => 'save', nowrap=>true
265
+ # = f.submit t('form.save'), :name => 'refresh', nowrap=>true
266
+ # = f.submit 'Print', :name => 'print', :class=>'print, nowrap=>true
267
+ #
268
+ # => <div class="actions">
269
+ # <span class='input big'>
270
+ # <input id="invoice_save" class="submit" type="submit" value="Save" name="save" />
271
+ # </span>
272
+ #
273
+ # <span class='custom class'>
274
+ # <input id="invoice_save" class="submit" type="submit" value="Save" name="save" />
275
+ # </span>
276
+ #
277
+ # <input id="invoice_save" class="submit" type="submit" value="Speichern" name="save" />
278
+ # <input id="invoice_refresh" class="submit" type="submit" value="Aktualisieren" name="refresh" />
279
+ # <input id="invoice_print" class="print" type="submit" value="Print" name="print" />
280
+ # </div>
281
+ #
282
+ #===Params
283
+ #Options Hash:
284
+ # :name is only needed if you have more than one button on the form
285
+ # :id is calculated based on the :name
286
+ # :class defaults to "submit"
287
+ def submit(value, options = {})
288
+ options[:id] ||= build_id(options[:name] || 'submit')
289
+ options[:class] ||= 'submit'
290
+
291
+ if !options.delete(:nowrap) #wrap submit in span so button can be formated with css
292
+ span_options = options.delete(:span) || {:class=>'input big'}
293
+ @template.capture_haml do
294
+ @template.haml_tag :span, span_options do
295
+ @template.haml_concat(super(value, options))
296
+ end
297
+ end
298
+ else #display field without span wrapping
299
+ super value, options
300
+ end
301
+ end
302
+
303
+
304
+ # Display a fields value as static text
305
+ #
306
+ # === Example haml
307
+ # = f.static_text :created_at
308
+ # = f.static_text :updated_at, :title => 'Last change'
309
+ # = f.static_text :title => 'Dummy text', :value => 'I´m a loonly dumb static text .. get me outa here!'
310
+ def static_text(fieldname, options = {})
311
+ if fieldname.is_a?(Hash) && options.empty? # Short call without fieldname
312
+ options = fieldname
313
+ fieldname = nil
314
+
315
+ # Value and title are needed
316
+ raise ArgumentError unless options[:title]
317
+ raise ArgumentError unless options[:value]
318
+ end
319
+
320
+ title = options.delete(:title) || build_title(fieldname)
321
+ value = options.delete(:value) || @template.formatted_value(current_object, fieldname)
322
+
323
+ if info_text = options.delete(:info) #add info tag if info test given
324
+ value << info_tag(info_text)
325
+ end
326
+ #keep existing class and add class right to wrapping element if its a money field
327
+ (options[:class] ||= '') << ' right' if fieldname && current_class.respond_to?("is_money_field") && current_class.is_money_field?(fieldname)
328
+ tag_wrapper title, value, options
329
+ end
330
+
331
+ end
332
+ end
333
+ end