awesome_form 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +113 -0
  4. data/Rakefile +23 -0
  5. data/app/views/awesome_form/default_theme/actions/_default.html.haml +8 -0
  6. data/app/views/awesome_form/default_theme/inputs/_association.html.haml +14 -0
  7. data/app/views/awesome_form/default_theme/inputs/_boolean.html.haml +12 -0
  8. data/app/views/awesome_form/default_theme/inputs/_check_boxes.html.haml +18 -0
  9. data/app/views/awesome_form/default_theme/inputs/_default.html.haml +11 -0
  10. data/app/views/awesome_form/default_theme/inputs/_radios.html.haml +18 -0
  11. data/app/views/awesome_form/default_theme/inputs/_select.html.haml +18 -0
  12. data/app/views/awesome_form/default_theme/inputs/_text.html.haml +9 -0
  13. data/app/views/awesome_form/default_theme/wrappers/_actions.html.haml +2 -0
  14. data/app/views/awesome_form/default_theme/wrappers/_boolean.html.haml +4 -0
  15. data/app/views/awesome_form/default_theme/wrappers/_default.html.haml +4 -0
  16. data/app/views/awesome_form/default_theme/wrappers/_inputs.html.haml +2 -0
  17. data/config/routes.rb +2 -0
  18. data/lib/awesome_form.rb +36 -0
  19. data/lib/awesome_form/action_view_extensions/form_helper.rb +13 -0
  20. data/lib/awesome_form/engine.rb +4 -0
  21. data/lib/awesome_form/form_builder.rb +63 -0
  22. data/lib/awesome_form/methods/actions.rb +78 -0
  23. data/lib/awesome_form/methods/attributes.rb +117 -0
  24. data/lib/awesome_form/methods/inputs.rb +64 -0
  25. data/lib/awesome_form/methods/labels.rb +18 -0
  26. data/lib/awesome_form/methods/naming.rb +32 -0
  27. data/lib/awesome_form/methods/rendering.rb +44 -0
  28. data/lib/awesome_form/version.rb +3 -0
  29. data/lib/tasks/awesome_form_tasks.rake +4 -0
  30. data/spec/dummy/README.rdoc +28 -0
  31. data/spec/dummy/Rakefile +6 -0
  32. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  33. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  34. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  35. data/spec/dummy/app/controllers/forms_controller.rb +3 -0
  36. data/spec/dummy/app/controllers/sessions_controller.rb +2 -0
  37. data/spec/dummy/app/controllers/universes_controller.rb +2 -0
  38. data/spec/dummy/app/controllers/users_controller.rb +2 -0
  39. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  40. data/spec/dummy/app/models/session.rb +3 -0
  41. data/spec/dummy/app/models/universe.rb +3 -0
  42. data/spec/dummy/app/models/user.rb +5 -0
  43. data/spec/dummy/app/views/forms/actions.html.haml +2 -0
  44. data/spec/dummy/app/views/forms/blocks.html.haml +6 -0
  45. data/spec/dummy/app/views/forms/check_boxes.html.haml +4 -0
  46. data/spec/dummy/app/views/forms/data.html.haml +2 -0
  47. data/spec/dummy/app/views/forms/fields_for.html.haml +9 -0
  48. data/spec/dummy/app/views/forms/inputs.html.haml +8 -0
  49. data/spec/dummy/app/views/forms/radios.html.haml +4 -0
  50. data/spec/dummy/app/views/forms/selects.html.haml +9 -0
  51. data/spec/dummy/app/views/forms/wrappers.html.haml +3 -0
  52. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  53. data/spec/dummy/app/views/sessions/new.html.haml +7 -0
  54. data/spec/dummy/app/views/universes/new.html.haml +5 -0
  55. data/spec/dummy/app/views/users/new.html.haml +5 -0
  56. data/spec/dummy/bin/bundle +3 -0
  57. data/spec/dummy/bin/rails +4 -0
  58. data/spec/dummy/bin/rake +4 -0
  59. data/spec/dummy/config.ru +4 -0
  60. data/spec/dummy/config/application.rb +28 -0
  61. data/spec/dummy/config/boot.rb +5 -0
  62. data/spec/dummy/config/database.yml +25 -0
  63. data/spec/dummy/config/environment.rb +5 -0
  64. data/spec/dummy/config/environments/development.rb +29 -0
  65. data/spec/dummy/config/environments/production.rb +80 -0
  66. data/spec/dummy/config/environments/test.rb +36 -0
  67. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  68. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  69. data/spec/dummy/config/initializers/inflections.rb +16 -0
  70. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  71. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  72. data/spec/dummy/config/initializers/session_store.rb +3 -0
  73. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  74. data/spec/dummy/config/locales/en.yml +23 -0
  75. data/spec/dummy/config/routes.rb +18 -0
  76. data/spec/dummy/db/development.sqlite3 +0 -0
  77. data/spec/dummy/db/migrate/20140108211716_create_users.rb +13 -0
  78. data/spec/dummy/db/migrate/20140108213444_create_universes.rb +11 -0
  79. data/spec/dummy/db/migrate/20140108214030_create_sessions.rb +10 -0
  80. data/spec/dummy/db/schema.rb +45 -0
  81. data/spec/dummy/db/test.sqlite3 +0 -0
  82. data/spec/dummy/log/development.log +14421 -0
  83. data/spec/dummy/log/test.log +84789 -0
  84. data/spec/dummy/public/404.html +58 -0
  85. data/spec/dummy/public/422.html +58 -0
  86. data/spec/dummy/public/500.html +57 -0
  87. data/spec/dummy/public/favicon.ico +0 -0
  88. data/spec/dummy/tmp/cache/assets/development/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  89. data/spec/dummy/tmp/cache/assets/development/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  90. data/spec/dummy/tmp/cache/assets/development/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  91. data/spec/dummy/tmp/cache/assets/development/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  92. data/spec/dummy/tmp/cache/assets/development/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  93. data/spec/dummy/tmp/cache/assets/development/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  94. data/spec/dummy/tmp/cache/assets/test/sprockets/13fe41fee1fe35b49d145bcc06610705 +0 -0
  95. data/spec/dummy/tmp/cache/assets/test/sprockets/2f5173deea6c795b8fdde723bb4b63af +0 -0
  96. data/spec/dummy/tmp/cache/assets/test/sprockets/357970feca3ac29060c1e3861e2c0953 +0 -0
  97. data/spec/dummy/tmp/cache/assets/test/sprockets/cffd775d018f68ce5dba1ee0d951a994 +0 -0
  98. data/spec/dummy/tmp/cache/assets/test/sprockets/d771ace226fc8215a3572e0aa35bb0d6 +0 -0
  99. data/spec/dummy/tmp/cache/assets/test/sprockets/f7cbd26ba1d28d48de824f0e94586655 +0 -0
  100. data/spec/features/actions_generation_spec.rb +13 -0
  101. data/spec/features/awesome_form_for_spec.rb +25 -0
  102. data/spec/features/blocks_handling_spec.rb +16 -0
  103. data/spec/features/classes_spec.rb +14 -0
  104. data/spec/features/fields_discovery_spec.rb +34 -0
  105. data/spec/features/inputs_generation_spec.rb +73 -0
  106. data/spec/features/wrappers_generation_spec.rb +36 -0
  107. data/spec/spec_helper.rb +35 -0
  108. data/spec/support/dom/dom_expression.rb +62 -0
  109. data/spec/support/factories/universes.rb +5 -0
  110. data/spec/support/features/match_content_of.rb +6 -0
  111. data/spec/units/awesome_form_spec.rb +11 -0
  112. metadata +263 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b455dd0dfebd90800af8b8bd3f331cbbe8be7008
4
+ data.tar.gz: 49f489364ef8e918c87dd4047880d846cf2e2c0c
5
+ SHA512:
6
+ metadata.gz: 9abc0cd5aac34858ded3e7573527a45ce92fd8b8db8d35353abbb3f1cefd8294bf344e05af978e208264fe75fc0e3901faa44f7567e7a01f14682f33545d86e3
7
+ data.tar.gz: 3042c0369946585e9ab2ade746bce45ab1fd8eae07ecf2977e659f85e9b89cafdca258f601c358afb9cfe9a7fed289766b0aa00762b11f5864c8598c7195725b
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,113 @@
1
+ ## AwesomeForm
2
+
3
+ [![Build Status](https://travis-ci.org/abe33/awesome_form.png)](https://travis-ci.org/abe33/awesome_form)
4
+ [![Coverage Status](https://coveralls.io/repos/abe33/awesome_form/badge.png)](https://coveralls.io/r/abe33/awesome_form)
5
+ [![Code Climate](https://codeclimate.com/github/abe33/awesome_form.png)](https://codeclimate.com/github/abe33/awesome_form)
6
+ [![Dependency Status](https://gemnasium.com/abe33/awesome_form.png)](https://gemnasium.com/abe33/awesome_form)
7
+
8
+ `AwesomeForm` is yet another form helper for `Rails`, but, contrary to `Formtastic` and `SimpleForm` it relies on partial views instead of html helpers.
9
+
10
+ Each input type is a partial in its theme directory, and wrappers are made using the `layout` option of the `render` method.
11
+
12
+ For instance, a boolean field will be rendered using a partial named `_boolean` in the `app/views/awesome_form` directory (See below for further explanation on partials lookup).
13
+
14
+ The same principles apply with form actions, each action is map to a partial file and eventually wrapped using a layout file.
15
+
16
+ ### Usage
17
+
18
+ Using `AwesomeForm` doesn't differ much of using `Formtastic` or `SimpleForm`:
19
+
20
+ ```haml
21
+ = awesome_form_for resource, url: resource_path(resource) do |form|
22
+
23
+ = form.inputs :attribute, :another_attribute
24
+
25
+ = form.input :some_attribute, as: :boolean
26
+
27
+ = form.actions :submit, :cancel
28
+ ```
29
+
30
+ ### Model Attributes Discovery
31
+
32
+ When using the `inputs` method without any arguments, the form builder will attempt to discover attributes of the model. By default it'll generate an input for every column of the model as well as for each association (both `belongs_to` and `has_many` associations).
33
+
34
+ Columns can be excluded by adding them to the `AwesomeForm.excluded_columns` array. By default, only timestamps columns are ignored.
35
+
36
+ ```ruby
37
+ AwesomeForm.setup do |config|
38
+ config.excluded_columns << %w(column_to_ignore another_column_to_ignore)
39
+ end
40
+ ```
41
+
42
+ By default, both `belongs_to` and `has_many` associations appears in discovered model's attributes. The default association are editable using
43
+ the `AwesomeForm.default_associations` config.
44
+
45
+ ```ruby
46
+ AwesomeForm.setup do |config|
47
+ config.default_associations = [:belongs_to]
48
+ end
49
+ ```
50
+
51
+ ### Views Lookup
52
+
53
+ The lookup process for a form input takes place as follow:
54
+
55
+ First, we look for a partial for the given model attribute, either
56
+ at the top level or in the selected theme:
57
+
58
+ 1. `app/views/awesome_form/inputs/:object_name/:attribute_name`
59
+ 2. `app/views/awesome_form/:theme/inputs/:object_name/:attribute_name`
60
+
61
+ If a partial cannot be found for the model attribute, we look for a
62
+ partial for the attribute's type:
63
+
64
+ 3. `app/views/awesome_form/inputs/:attribute_type`
65
+ 4. `app/views/awesome_form/:theme/inputs/:attribute_type`
66
+ 5. `app/views/awesome_form/default_theme/inputs/:attribute_type`
67
+
68
+ And if there's no partial for the attribute's type, we look for the
69
+ default input partial.
70
+
71
+ 6. `app/views/awesome_form/inputs/default`
72
+ 7. `app/views/awesome_form/:theme/inputs/default`
73
+ 8. `app/views/awesome_form/default_theme/inputs/default`
74
+
75
+ #### Inputs Wrappers
76
+
77
+ The same kind of lookup is used for fields wrappers, but instead of looking into the `inputs` view directory, it'll look into the `wrappers` directory.
78
+
79
+ 1. `app/views/awesome_form/wrappers/:object_name/:attribute_name`
80
+ 2. `app/views/awesome_form/:theme/wrappers/:object_name/:attribute_name`
81
+ 3. `app/views/awesome_form/wrappers/:attribute_type`
82
+ 4. `app/views/awesome_form/:theme/wrappers/:attribute_type`
83
+ 5. `app/views/awesome_form/default_theme/wrappers/:attribute_type`
84
+ 6. `app/views/awesome_form/wrappers/default`
85
+ 7. `app/views/awesome_form/:theme/wrappers/default`
86
+ 8. `app/views/awesome_form/default_theme/wrappers/default`
87
+
88
+ Fields wrappers are just layouts, and as every layout, the content of the view is injected where the `yield` keyword is found in the layout.
89
+
90
+ #### Action Views Lookup
91
+
92
+ A similar lookup is performed to find actions's views and wrappers:
93
+
94
+ For the action itself:
95
+
96
+ 1. `awesome_form/actions/:object_name/:action`
97
+ 2. `awesome_form/:theme/actions/:object_name/:action`
98
+ 3. `awesome_form/actions/:action`
99
+ 4. `awesome_form/:theme/actions/:action`
100
+ 5. `awesome_form/default_theme/actions/:action`
101
+ 6. `awesome_form/actions/default`
102
+ 7. `awesome_form/:theme/actions/default`
103
+ 8. `awesome_form/default_theme/actions/default`
104
+
105
+ And for the wrapper:
106
+
107
+ 1. `awesome_form/wrappers/:object_name/:action`
108
+ 2. `awesome_form/:theme/wrappers/:object_name/:action`
109
+ 3. `awesome_form/wrappers/:action`
110
+ 4. `awesome_form/:theme/wrappers/:action`
111
+ 5. `awesome_form/default_theme/wrappers/:action`
112
+
113
+ As you may notice, the action wrapper lookup doesn't fallback on any default.
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'AwesomeForm'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
@@ -0,0 +1,8 @@
1
+ :ruby
2
+ attrs = {
3
+ type: action,
4
+ name: name,
5
+ class: "#{builder.model_name} #{action}",
6
+ }
7
+
8
+ %button.action{attrs}= builder.action_label(action)
@@ -0,0 +1,14 @@
1
+ :ruby
2
+ attrs = {
3
+ name: builder.association_name(attribute_name),
4
+ id: builder.input_id(attribute_name),
5
+ class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
6
+ multiple: multiple,
7
+ }
8
+
9
+ %select.input{attrs}
10
+ - if defined?(include_blank) && include_blank
11
+ %option{value: ''}
12
+
13
+ - collection.each do |value, label|
14
+ %option{value: value, selected: selected.include?(value)}= label
@@ -0,0 +1,12 @@
1
+ :ruby
2
+ attrs = {
3
+ name: builder.input_name(attribute_name),
4
+ id: builder.input_id(attribute_name),
5
+ type: :checkbox,
6
+ class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
7
+ value: '1',
8
+ checked: object.try(attribute_name),
9
+ }
10
+ attrs.merge! builder.filter_attributes_for(:input, input_html)
11
+
12
+ %input.input{attrs}
@@ -0,0 +1,18 @@
1
+ - selected ||= []
2
+
3
+ - collection.each do |value, label|
4
+ :ruby
5
+ id = "#{builder.input_id(attribute_name)}_#{(label || value).to_s.underscore}"
6
+ attrs = {
7
+ name: builder.collection_name(attribute_name),
8
+ id: id,
9
+ type: :checkbox,
10
+ class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
11
+ value: value,
12
+ checked: selected.include?(value),
13
+ }
14
+ attrs.merge! builder.filter_attributes_for(:input, input_html)
15
+
16
+ %p
17
+ %input.input{attrs}
18
+ %label{for: id}= label || value.to_s.humanize
@@ -0,0 +1,11 @@
1
+ :ruby
2
+ attrs = {
3
+ name: builder.input_name(attribute_name),
4
+ id: builder.input_id(attribute_name),
5
+ class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
6
+ type: type == :string ? :text : type,
7
+ value: object.try(attribute_name),
8
+ }
9
+ attrs.merge! builder.filter_attributes_for(:input, input_html)
10
+
11
+ %input.input{attrs}
@@ -0,0 +1,18 @@
1
+ - selected ||= []
2
+
3
+ - collection.each do |value, label|
4
+ :ruby
5
+ id = "#{builder.input_id(attribute_name)}_#{(label || value).to_s.underscore}"
6
+ attrs = {
7
+ name: builder.input_name(attribute_name),
8
+ id: id,
9
+ type: :radio,
10
+ class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
11
+ value: value,
12
+ checked: selected.include?(value),
13
+ }
14
+ attrs.merge! builder.filter_attributes_for(:input, input_html)
15
+
16
+ %p
17
+ %input.input{attrs}
18
+ %label{for: id}= label || value.to_s.humanize
@@ -0,0 +1,18 @@
1
+ :ruby
2
+ multiple ||= false
3
+ selected ||= []
4
+
5
+ attrs = {
6
+ name: multiple ? builder.collection_name(attribute_name) : builder.input_name(attribute_name),
7
+ id: builder.input_id(attribute_name),
8
+ class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
9
+ multiple: multiple
10
+ }
11
+ attrs.merge! builder.filter_attributes_for(:select, input_html)
12
+
13
+ %select.input{attrs}
14
+ - if defined?(include_blank) && include_blank
15
+ %option{value: ''}
16
+
17
+ - collection.each do |value, label|
18
+ %option{value: value, selected: selected.include?(value)}= label || value.to_s.humanize
@@ -0,0 +1,9 @@
1
+ :ruby
2
+ attrs = {
3
+ name: builder.input_name(attribute_name),
4
+ class: "#{builder.model_name} #{attribute_name} #{input_html.delete(:class)}",
5
+ id: builder.input_id(attribute_name),
6
+ }
7
+ attrs.merge! builder.filter_attributes_for(:textarea, input_html)
8
+
9
+ %textarea.input{attrs}= object.try(attribute_name)
@@ -0,0 +1,2 @@
1
+
2
+ %fieldset.actions= yield
@@ -0,0 +1,4 @@
1
+ .field
2
+ = yield
3
+ %label{for: builder.input_id(attribute_name)}
4
+ = builder.input_label(attribute_name)
@@ -0,0 +1,4 @@
1
+ .field
2
+ %label{for: builder.input_id(attribute_name)}
3
+ = builder.input_label(attribute_name)
4
+ .controls= yield
@@ -0,0 +1,2 @@
1
+
2
+ %fieldset.inputs= yield
@@ -0,0 +1,2 @@
1
+ Rails.application.routes.draw do
2
+ end
@@ -0,0 +1,36 @@
1
+ require 'action_view'
2
+ require "awesome_form/engine"
3
+ require "awesome_form/form_builder"
4
+ require "awesome_form/action_view_extensions/form_helper"
5
+
6
+ module AwesomeForm
7
+ extend ActiveSupport::Autoload
8
+
9
+ eager_autoload do
10
+ autoload :FormBuilder
11
+ end
12
+
13
+ mattr_accessor :theme
14
+ @@theme = :default_theme
15
+
16
+ mattr_accessor :default_actions
17
+ @@default_actions = [:submit]
18
+
19
+ mattr_accessor :excluded_columns
20
+ @@excluded_columns = [:created_at, :updated_at]
21
+
22
+ mattr_accessor :default_associations
23
+ @@default_associations = [:belongs_to, :has_many]
24
+
25
+ mattr_accessor :legal_attributes
26
+ @@legal_attributes = {
27
+ input: %w(accept alt autocomplete autofocus checked dirname disabled form formaction formenctype formmethod formnovalidate formtarget height list max maxlength min multiple name pattern placeholder readonly required size src step type value width).map(&:to_sym),
28
+ textarea: %w(autocomplete autofocus cols dirname disabled form maxlength name placeholder readonly required rows wrap).map(&:to_sym),
29
+ select: %w(autofocus disabled form multiple name required size).map(&:to_sym)
30
+ }
31
+
32
+ def self.setup
33
+ yield self
34
+ end
35
+ end
36
+
@@ -0,0 +1,13 @@
1
+ module AwesomeForm
2
+ module ActionViewExtensions
3
+ module FormHelper
4
+ def awesome_form_for(object, options={}, &block)
5
+ options[:builder] ||= AwesomeForm::FormBuilder
6
+
7
+ form_for(object, options, &block)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+ ActionView::Base.send :include, AwesomeForm::ActionViewExtensions::FormHelper
@@ -0,0 +1,4 @@
1
+ module AwesomeForm
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,63 @@
1
+ require 'awesome_form/methods/attributes'
2
+ require 'awesome_form/methods/inputs'
3
+ require 'awesome_form/methods/actions'
4
+ require 'awesome_form/methods/naming'
5
+ require 'awesome_form/methods/labels'
6
+
7
+ module AwesomeForm
8
+ class FormBuilder < ActionView::Helpers::FormBuilder
9
+ include AwesomeForm::Methods::Attributes
10
+ include AwesomeForm::Methods::Inputs
11
+ include AwesomeForm::Methods::Actions
12
+ include AwesomeForm::Methods::Naming
13
+ include AwesomeForm::Methods::Labels
14
+
15
+ def initialize(*)
16
+ super
17
+ end
18
+
19
+ def render(render_options)
20
+ @template.render(render_options)
21
+ end
22
+
23
+ def view_exists?(view)
24
+ path_elements = view.split('/')
25
+ view = "_#{path_elements.pop}".squeeze '_'
26
+ prefix = path_elements.join('/')
27
+
28
+ @template.lookup_context.exists? view, [prefix]
29
+ end
30
+
31
+ def lookup_views(paths)
32
+ paths.select { |p| view_exists? p }.first
33
+ end
34
+
35
+ def filter_attributes_for(html, options)
36
+ options.select do |k|
37
+ AwesomeForm.legal_attributes[html].include?(k) ||
38
+ k.to_s =~ /^data($|-)/
39
+ end
40
+ end
41
+
42
+ def model_name
43
+ object.class.name.underscore
44
+ end
45
+
46
+ def resource_name
47
+ model_name.pluralize
48
+ end
49
+
50
+ protected
51
+
52
+ def filter_arguments(*args)
53
+ options = {}
54
+
55
+ symbols = args.select {|a| a.is_a? Symbol }
56
+ options_args = args.select {|a| a.is_a? Hash }
57
+ options_args.each {|h| options.merge! h }
58
+
59
+ [symbols, options]
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,78 @@
1
+ require 'awesome_form/methods/rendering'
2
+
3
+ module AwesomeForm
4
+ module Methods
5
+ module Actions
6
+ extend AwesomeForm::Methods::Rendering
7
+
8
+ render_method :action
9
+
10
+ protected
11
+
12
+ def default_keys_for_actions
13
+ AwesomeForm.default_actions
14
+ end
15
+
16
+ def options_for_action(action, options)
17
+ options = {
18
+ action: action.to_sym,
19
+ object_name: object_name,
20
+ object: object,
21
+ builder: self
22
+ }
23
+
24
+ case action.to_sym
25
+ when :submit then options[:name] = :commit
26
+ when :cancel then options[:name] = :cancel
27
+ when :reset then options[:name] = :reset
28
+ end
29
+
30
+ options.merge(options)
31
+ end
32
+
33
+ def default_actions_content(options)
34
+ ''
35
+ end
36
+
37
+ def partial_for_action(action_options)
38
+ partial_paths_for_action(action_options).select { |p| view_exists? p }.first
39
+ end
40
+
41
+ def wrapper_for_action(action_options)
42
+ partial_paths_for_action_wrapper(action_options).select { |p| view_exists? p }.first
43
+ end
44
+
45
+ def partial_paths_for_action(action_options)
46
+ theme = AwesomeForm.theme
47
+ action = action_options[:action]
48
+
49
+ [
50
+ "awesome_form/actions/#{resource_name}/#{action}",
51
+ "awesome_form/#{theme}/actions/#{resource_name}/#{action}",
52
+
53
+ "awesome_form/actions/#{action}",
54
+ "awesome_form/#{theme}/actions/#{action}",
55
+ "awesome_form/default_theme/actions/#{action}",
56
+
57
+ "awesome_form/actions/default",
58
+ "awesome_form/#{theme}/actions/default",
59
+ "awesome_form/default_theme/actions/default"
60
+ ].uniq
61
+ end
62
+
63
+ def partial_paths_for_action_wrapper(action_options)
64
+ theme = AwesomeForm.theme
65
+ action = action_options[:action]
66
+
67
+ [
68
+ "awesome_form/wrappers/#{resource_name}/#{action}",
69
+ "awesome_form/#{theme}/wrappers/#{resource_name}/#{action}",
70
+
71
+ "awesome_form/wrappers/#{action}",
72
+ "awesome_form/#{theme}/wrappers/#{action}",
73
+ "awesome_form/default_theme/wrappers/#{action}"
74
+ ].uniq
75
+ end
76
+ end
77
+ end
78
+ end