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,146 @@
1
+ module KingForm
2
+ module Builder
3
+ ##########################################################################
4
+ # Modified core html tag methods.
5
+ module FormFieldsOverrides
6
+ # private
7
+ def text_field(method, opts = {})
8
+ opts[:class] = (opts[:class] || '') + ' text'
9
+ info_text = opts.delete(:info)
10
+ super(method, opts) + info_tag(info_text || method)
11
+ end
12
+
13
+ def text_area(method, opts = {})
14
+ opts[:size] ||= "20x3" # sized needed for valid html
15
+ info_text = opts.delete(:info)
16
+ super(method, opts) + info_tag(info_text || method)
17
+ end
18
+
19
+ def check_box(method, opts = {})
20
+ info_text = opts.delete(:info)
21
+ super(method, opts) + info_tag(info_text || method)
22
+ end
23
+
24
+ def radio_button(method, tag_value, opts = {})
25
+ info_text = opts.delete(:info)
26
+ super(method, tag_value, opts) + info_tag(info_text || method)
27
+ end
28
+
29
+ def select(method, choices, opts = {}, html_opts = {})
30
+ info_text = opts.delete(:info)
31
+ super(method, choices || [], opts, html_opts) + info_tag(info_text || method)
32
+ end
33
+
34
+ def date_select(method, opts = {}, html_opts = {})
35
+ info_text = opts.delete(:info)
36
+ super(method, opts, html_opts) + info_tag(info_text || method)
37
+ end
38
+
39
+ def password_field(method, opts = {})
40
+ info_text = opts.delete(:info)
41
+ super(method, opts) + info_tag(info_text || method)
42
+ end
43
+
44
+ def file_field(method, opts = {})
45
+ info_text = opts.delete(:info)
46
+ super(method, opts) + info_tag(info_text || method)
47
+ end
48
+
49
+ ###########
50
+
51
+ def text_field_tag(name, value = nil, opts = {})
52
+ opts[:class] = (opts[:class] || '') + ' text'
53
+ name, infos, opts = build_id_name_info(name, opts)
54
+ @template.text_field_tag(name, value, opts) + infos
55
+ end
56
+
57
+ # Create a hidden field tag and construct its fieldname (object[name]) from
58
+ # the current object
59
+ # When the name is beeing passed in as string its just taken like it is
60
+ # ==== Parameter
61
+ # same as hidden_field_tag in Rails plus:
62
+ # name<String>:: The name is passed right thought to hidden_field_tag
63
+ # name<Symbol>:: The name is put together with current object => object[name]
64
+ def hidden_field_tag(name, value = nil, opts = {})
65
+ name, infos, opts = build_id_name_info(name, opts)
66
+ @template.hidden_field_tag(name, value, opts) # obviously no infos
67
+ end
68
+
69
+ def password_field_tag(name = "password", value = nil, opts = {})
70
+ opts[:class] = (opts[:class] || '') + ' text'
71
+ name, infos, opts = build_id_name_info(name, opts)
72
+ @template.password_field_tag(name, value, opts) + infos
73
+ end
74
+
75
+ # Overide the native filefield tag
76
+ # ==== Parameter
77
+ # name<(Symbol,String)>:: The name for the field. when given as symbol, the
78
+ # name is constructed for the current object oject[name] and an id is build.
79
+ # If passed as string, the name is taken right away and no auto id is created
80
+ # opts<Hash{Symbol=>String}>:: All file_field opts +
81
+ # :info which is taken for the help text
82
+ def file_field_tag(name, opts = {})
83
+ opts[:class] ||= nil
84
+ name, infos, opts = build_id_name_info(name, opts)
85
+ @template.file_field_tag(name, opts) + infos
86
+ end
87
+
88
+ def text_area_tag(name, value, opts = {})
89
+ opts[:size] ||= "20x3" # sized needed for valid html
90
+ name, infos, opts = build_id_name_info(name, opts)
91
+ @template.text_area_tag(name, value, opts) + infos
92
+ end
93
+
94
+ def check_box_tag(name, value = "1", checked = false, opts = {})
95
+ name, infos, opts = build_id_name_info(name, opts)
96
+ @template.check_box_tag(name, value, checked, opts) + infos
97
+ end
98
+
99
+ # Overriden rails select_tag
100
+ # Constructs the fieldname
101
+ # ==== Example
102
+ # - dl_fields_for @user do |u|
103
+ # = u.selection :project_id, :choices => @projects, :title => "Projects"
104
+ # = u.selection "user[custom_field]", :choices => @some_choices
105
+ #
106
+ # ==== Parameter
107
+ # name<Symbol, String>:: If symbol: the name and id is auto-constructed object[field_name]
108
+ # and the info tag is build from translation see info_tag.
109
+ # If String: The name is taken as it is, no id and info is auto-created.
110
+ # They must be passed in as opts[:id], opts[:info]
111
+ # option_tags<String>:: The opts as html for the select
112
+ # opts<Hash{Symbol=>String}>:: Rails select_tag opts + :info
113
+ def select_tag(name, option_tags = nil, opts = {})
114
+ name, infos, opts = build_id_name_info(name, opts)
115
+ @template.select_tag(name, option_tags, opts) + infos
116
+ end
117
+
118
+ # Builds name, opts, infos according to the given name type
119
+ # ==== Parameter
120
+ # name<Symbol>::
121
+ # - id for an element is build => client_name_id
122
+ # - info tag is looked up in I18n => see KingForm::Builder::Base info_tag
123
+ # - fieldname is assumed to belong to an object => client[name]
124
+ # name<String>::
125
+ # - id for an element must be present in opts[:id]
126
+ # - info tag is looked up in I18n when opts[:info] is a symbol, else string is taken #KingForm::Builder::Base info_tag
127
+ # - fieldname is assumed to belong to an object => client[name]
128
+ #
129
+ def build_id_name_info(name, opts)
130
+ if name.is_a?(Symbol)
131
+ opts[:id] ||= build_id(name)
132
+ # build info tag, cause info_tag(:symbol) is looking into I18n transl
133
+ infos = info_tag( opts.delete(:info) || name)
134
+ #now set real name as string
135
+ name = "#{@object_name}[#{name}]"
136
+ else
137
+ opts[:id] ||= nil
138
+ info_text = opts.delete(:info)
139
+ infos = info_text ? info_tag( info_text) : ''
140
+ end
141
+ [name, infos, opts ]
142
+ end
143
+
144
+ end
145
+ end
146
+ end
@@ -0,0 +1,116 @@
1
+ module KingForm
2
+ module Builder
3
+ # Create forms with a fieldset->div->label->input structure
4
+ # ==== Example haml
5
+ # - labeled_form_for(current_object) do |f|
6
+ # - f.section _('legend.user.details') do
7
+ # = f.text :first_name
8
+ # = f.text :last_name
9
+ #
10
+ # => #<form ..>
11
+ # <fieldset>
12
+ # <legend>User Details</legend>
13
+ # <div>
14
+ # <label>Firstname</label>
15
+ # <input type='text' value='Otto'/>
16
+ # </div>
17
+ # <div>
18
+ # <label>Lastname</label>
19
+ # <input type='text' value='Bismark'/>
20
+ # </div>
21
+ # </fieldset>
22
+ # </form>
23
+ #
24
+ class Labeled < KingForm::Builder::Base
25
+ # Create a section(fieldset) within a form
26
+ # A section is a group of related object information with name/value pairs,
27
+ # like all dates of an object or the users name fields(last/first/title/nick).
28
+ #
29
+ # A section html consists of a fieldset > legend > div > label > input
30
+ # The dt holds the title/description (DefinitionType) of the current field
31
+ # The dd holds the value.
32
+ # This wrapup is preferred over ul/li or other listing types because of
33
+ # the semantic meaning of the html
34
+ #
35
+ #===Example haml
36
+ # - f.section _('legend.user.details') do
37
+ # = f.text :first_name
38
+ # = f.text :last_name
39
+ #
40
+ # => # <fieldset>
41
+ # <legend>User Details</legend>
42
+ # <div>
43
+ # <label>Firstname</label>
44
+ # <input type='text' value='Otto'/>
45
+ # </div>
46
+ # <div>
47
+ # <label>Lastname</label>
48
+ # <input type='text' value='Bismark'/>
49
+ # </div> #
50
+ # </fieldset>
51
+ #
52
+ def section(title = nil, options = {}, &block)
53
+ raise ArgumentError if title && !title.is_a?(String)
54
+ @template.haml_tag :fieldset, options do
55
+ @template.haml_tag :legend, title unless title.blank?
56
+ @template.haml_concat( @template.capture_haml(&block) )
57
+ end
58
+ end
59
+
60
+ # Show multiple inputs in one line (div tag)
61
+ # === Example haml
62
+ # - f.bundle _('Gender and Title') do
63
+ # = f.selection :gender
64
+ # = f.text :title, :medium
65
+ # ==== Parameter
66
+ # title<String>:: The name used as label
67
+ def bundle(title = nil, options = {}, &block)
68
+ @config[:bundle] = true
69
+ @bundle_counter = 0
70
+ tags = @template.capture(&block)
71
+ @config[:bundle] = false
72
+ @template.concat( tag_wrapper(title, tags, options) )
73
+ end
74
+
75
+ # Add titles/labels to input tag and wrap in div
76
+ #
77
+ # ==== Parameter
78
+ # fieldname_or_title<String Symbol>:: The title for the field
79
+ # tags<String>:: html tags as string
80
+ # options<Hash{Symbold=>String}>:: options for the surrounding html
81
+ # ==== Options
82
+ # :label => options for label
83
+ # :div => options for surrounding div
84
+ # :align => alignment in table
85
+ def tag_wrapper(fieldname_or_title, tags, options = {})
86
+ if @config[:bundle]
87
+ @bundle_counter += 1
88
+ tags
89
+ elsif @config[:table] # called from "table" => build a table cell (td)
90
+ # Only in first row: Build column header
91
+ if @config[:row_number] == 1
92
+ @config[:column_header].push :title => build_title(fieldname_or_title),
93
+ :options => { :align => options[:align] || 'left' }
94
+ end
95
+ @template.capture_haml do
96
+ @template.haml_tag(:td, tags, options)
97
+ end
98
+ else
99
+ out = if tags.match /checkbox/ # wrap only checkbox tag into to label, so it is clickable
100
+ label_tag(fieldname_or_title + tags, options[:label])
101
+ else # other tags stay outside label tag, because they don't like to be wrapped sometimes
102
+ label_tag(fieldname_or_title, options[:label]) + tags
103
+ end
104
+ "<div> #{out}</div>"
105
+ end
106
+ end
107
+
108
+ # Build a label tag
109
+ #TODO enhance with option <label for="fieldname">
110
+ def label_tag(fieldname_or_title, options = {})
111
+ fieldname_or_title.blank? ? "" : content_tag(:label, build_title(fieldname_or_title), options)
112
+ end
113
+
114
+ end # labeled class
115
+ end #module
116
+ end#module
@@ -0,0 +1,97 @@
1
+ module KingForm
2
+ module Helper
3
+
4
+ # renders a form with the # KingForm::Builder::DefinitionList
5
+ # It allows to devide the form into sections(fieldsets) where each contains
6
+ # a definition list with dl/dd blocks for each label/field
7
+ #
8
+ # Read on to find out more about the available tags/fieldtypes
9
+ #
10
+ # === Example haml
11
+ # -dl_form_for(:client, :url => object_url, :html => { :method => :put }) do |f|
12
+ # - f.section 'Client Details' do
13
+ # = f.text :number
14
+ # - f.bundle 'Gender/Title' do
15
+ # = f.selection :gender
16
+ # = f.text :title, :class => 'medium'
17
+ # = f.text :position
18
+ # = f.text :last_name
19
+ # = f.date :birthday
20
+ # # =><form .. method..> <fieldset>
21
+ # <legend>Client Details</legend>
22
+ # <dl>
23
+ # <dt>Number</dt>
24
+ # <dd><input name=client[number] type=text></dd>
25
+ # ....
26
+ # </dl>
27
+ # </fieldset></form>
28
+ #
29
+ def dl_form_for(record_or_name_or_array, *args, &proc)
30
+ options = args.last.is_a?(Hash) ? args.pop : {}
31
+ options[:builder] = KingForm::Builder::DefinitionList
32
+ form_for(record_or_name_or_array, *(args << options), &proc)
33
+ end
34
+
35
+ def dl_fields_for(record_or_name_or_array, *args, &block)
36
+ options = args.last.is_a?(Hash) ? args.pop : {}
37
+ options[:builder] = KingForm::Builder::DefinitionList
38
+ fields_for(record_or_name_or_array, *(args << options), &block)
39
+ end
40
+
41
+ # renders a form with the KingForm::Builder::Labeled
42
+ # It allows to devide the form into sections(fieldsets) where each contains
43
+ # a definition list with dl/dd blocks for each label/field
44
+ #
45
+ # Read on to find out more about the avalable tags/fieldtypes
46
+ #
47
+ # === Example haml
48
+ # -labeled_form_for(:client, :url => object_url, :html => { :method => :put }) do |f|
49
+ # - f.section 'Client Details' do
50
+ # = f.text :number
51
+ # - f.bundle 'Gender/Title' do
52
+ # = f.text :gender
53
+ # = f.text :title, :class => 'medium'
54
+ # # =><form ...>
55
+ # <fieldset>
56
+ # <legend>Client Details</legend>
57
+ # <div>
58
+ # <label>Number </label>
59
+ # <input name=client[number] type=text>
60
+ # </div>
61
+ # <div>
62
+ # <label>Gender/Title</label>
63
+ # <input type='text' name='client[gender]' value='male'/>
64
+ # <input type='text' name='client[title]' value='Prof.'/>
65
+ # </div>
66
+ # </fieldset>
67
+ # </form>
68
+ #
69
+ def labeled_form_for(record_or_name_or_array, *args, &proc)
70
+ options = args.last.is_a?(Hash) ? args.pop : {}
71
+ options[:builder] = KingForm::Builder::Labeled
72
+ form_for(record_or_name_or_array, *(args << options), &proc)
73
+ end
74
+
75
+ def labeled_fields_for(record_or_name_or_array, *args, &block)
76
+ options = args.last.is_a?(Hash) ? args.pop : {}
77
+ options[:builder] = KingForm::Builder::Labeled
78
+ fields_for(record_or_name_or_array, *(args << options), &block)
79
+ end
80
+
81
+ # Returns an array for a given settings which has comma-seperated values.
82
+ # In the view those are used for select boxes
83
+ # Accepts an optional block to change the array elements
84
+ def make_select(values, &block)
85
+ return nil unless values
86
+ raise ArgumentError unless values.class == String
87
+
88
+ result = []
89
+ values.split(',').each do |s|
90
+ s.strip!
91
+ s = yield(s) if block_given?
92
+ result.push(s)
93
+ end
94
+ result
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,61 @@
1
+ module KingForm
2
+ # was attribute_fu with changes from: http://github.com/odadata/attribute_fu/commit/a0402d12f2d380d8decc82bdfbcc5d8a0a185524
3
+ # Methods for building forms that contain fields for associated models.
4
+ module NestedFormHelper
5
+
6
+ # Renders the form for nested objects defined via activerecord accepts_nested_attributes_for
7
+ #
8
+ # The associated argument can be either an object, or a collection of objects to be rendered.
9
+ #
10
+ # An options hash can be specified to override the default behaviors.
11
+ #
12
+ # Options are:
13
+ # * <tt>:new</tt> - specify a certain number of new elements to be added to the form. Useful for displaying a
14
+ # few blank elements at the bottom.
15
+ # * <tt>:name</tt> - override the name of the association, both for the field names, and the name of the partial
16
+ # * <tt>:partial</tt> - specify the name of the partial in which the form is located.
17
+ # * <tt>:fields_for</tt> - specify additional options for the fields_for_associated call
18
+ # * <tt>:locals</tt> - specify additional variables to be passed along to the partial
19
+ # * <tt>:render</tt> - specify additional options to be passed along to the render :partial call
20
+ # * <tt>:skip</tt> - array of elements which will be skipped, usefull if you already rendered a partial in the same form with parts of the data.
21
+ # eg. obj.addresses, render the firt address on top of form, render all the other addresses at the bottom
22
+ #
23
+ def render_nested_form(associated, opts = {})
24
+ associated = associated.is_a?(Array) ? associated : [associated] # preserve association proxy if this is one
25
+ opts.symbolize_keys!
26
+ (opts[:new] - associated.select(&:new_record?).length).times { associated.build } if opts[:new]
27
+
28
+ unless associated.empty?
29
+ name = extract_option_or_class_name(opts, :name, associated.first)
30
+ partial = opts[:partial] || name
31
+ if opts[:skip] # objects to be skipped are present
32
+ skip_el = opts[:skip].is_a?(Array) ? opts[:skip] : [opts[:skip]]
33
+ assoc_el = []
34
+ associated.each { |el| assoc_el << el unless skip_el.include?(el) }
35
+ else # normal procedure
36
+ assoc_el = associated
37
+ end
38
+
39
+ output = assoc_el.map do |element|
40
+ fields_for(association_name(name), element, (opts[:fields_for] || {}).merge(:name => name)) do |f|
41
+
42
+ @template.render( {:partial => "#{partial}",
43
+ #The current objects classname is always present in partial so:
44
+ #when Object is LineItem locals has :line_item => object
45
+ :locals => {name.to_sym => f.object, :f => f}.merge( opts[:locals] || {} )
46
+ }.merge( opts[:render] || {} ) )
47
+ end
48
+ end
49
+ output.join
50
+ end
51
+ end
52
+
53
+ private
54
+ def association_name(class_name)
55
+ @object.respond_to?("#{class_name}_attributes=") ? class_name : class_name.pluralize
56
+ end
57
+ def extract_option_or_class_name(hash, option, object)
58
+ (hash.delete(option) || object.class.name.split('::').last.underscore).to_s
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,25 @@
1
+ # override rails extra_tags_for_form method wich creates hidden special form
2
+ # fields -> method and auth_token
3
+ # so they don't use inline styles anymore
4
+ # From:
5
+ # <div style="margin:0;padding:0"><input name="authenticity_token"
6
+ # To:
7
+ # <div><input name="authenticity_token"
8
+ module ActionView::Helpers::FormTagHelper
9
+ private
10
+ # overridden method to kill inline styles
11
+ # from actionpack-2.1.0/lib/action_view/helpers/form_tag_helper.rb
12
+ def extra_tags_for_form(html_options)
13
+ case method = html_options.delete("method").to_s
14
+ when /^get$/i # must be case-insentive, but can't use downcase as might be nil
15
+ html_options["method"] = "get"
16
+ ''
17
+ when /^post$/i, "", nil
18
+ html_options["method"] = "post"
19
+ protect_against_forgery? ? content_tag(:div, token_tag) : ''
20
+ else
21
+ html_options["method"] = "post"
22
+ content_tag(:div, tag(:input, :type => "hidden", :name => "_method", :value => method) + token_tag)
23
+ end
24
+ end
25
+ end