activemodel-form 0.0.1

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.
Files changed (46) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +3 -0
  3. data/LICENSE.txt +22 -0
  4. data/README.md +71 -0
  5. data/Rakefile +16 -0
  6. data/activemodel-form.gemspec +25 -0
  7. data/example/.gitignore +15 -0
  8. data/example/Gemfile +13 -0
  9. data/example/README.md +9 -0
  10. data/example/app/controllers/application_controller.rb +3 -0
  11. data/example/app/controllers/forms_controller.rb +25 -0
  12. data/example/app/views/forms/new.html.erb +24 -0
  13. data/example/app/views/layouts/application.html.erb +13 -0
  14. data/example/config/application.rb +62 -0
  15. data/example/config/boot.rb +6 -0
  16. data/example/config/database.yml +11 -0
  17. data/example/config/environment.rb +5 -0
  18. data/example/config/environments/development.rb +37 -0
  19. data/example/config/environments/production.rb +67 -0
  20. data/example/config/environments/test.rb +37 -0
  21. data/example/config/initializers/backtrace_silencers.rb +7 -0
  22. data/example/config/initializers/formtastic.rb +90 -0
  23. data/example/config/initializers/inflections.rb +15 -0
  24. data/example/config/initializers/mime_types.rb +5 -0
  25. data/example/config/initializers/secret_token.rb +7 -0
  26. data/example/config/initializers/session_store.rb +8 -0
  27. data/example/config/initializers/simple_form.rb +138 -0
  28. data/example/config/initializers/wrap_parameters.rb +14 -0
  29. data/example/config/routes.rb +8 -0
  30. data/example/config.ru +4 -0
  31. data/example/public/404.html +26 -0
  32. data/example/public/422.html +26 -0
  33. data/example/public/500.html +25 -0
  34. data/example/public/favicon.ico +0 -0
  35. data/example/public/robots.txt +5 -0
  36. data/example/script/rails +6 -0
  37. data/lib/active_model/form/attributes.rb +107 -0
  38. data/lib/active_model/form/version.rb +5 -0
  39. data/lib/active_model/form.rb +85 -0
  40. data/lib/activemodel-form.rb +1 -0
  41. data/spec/boolean_attribute_form_spec.rb +24 -0
  42. data/spec/date_time_form_spec.rb +27 -0
  43. data/spec/integer_attribute_form_spec.rb +24 -0
  44. data/spec/model_name_spec.rb +17 -0
  45. data/spec/spec_helper.rb +6 -0
  46. metadata +165 -0
@@ -0,0 +1,138 @@
1
+ # Use this setup block to configure all options available in SimpleForm.
2
+ SimpleForm.setup do |config|
3
+ # Wrappers are used by the form builder to generate a
4
+ # complete input. You can remove any component from the
5
+ # wrapper, change the order or even add your own to the
6
+ # stack. The options given below are used to wrap the
7
+ # whole input.
8
+ config.wrappers :default, :class => :input,
9
+ :hint_class => :field_with_hint, :error_class => :field_with_errors do |b|
10
+ ## Extensions enabled by default
11
+ # Any of these extensions can be disabled for a
12
+ # given input by passing: `f.input EXTENSION_NAME => false`.
13
+ # You can make any of these extensions optional by
14
+ # renaming `b.use` to `b.optional`.
15
+
16
+ # Determines whether to use HTML5 (:email, :url, ...)
17
+ # and required attributes
18
+ b.use :html5
19
+
20
+ # Calculates placeholders automatically from I18n
21
+ # You can also pass a string as f.input :placeholder => "Placeholder"
22
+ b.use :placeholder
23
+
24
+ ## Optional extensions
25
+ # They are disabled unless you pass `f.input EXTENSION_NAME => :lookup`
26
+ # to the input. If so, they will retrieve the values from the model
27
+ # if any exists. If you want to enable the lookup for any of those
28
+ # extensions by default, you can change `b.optional` to `b.use`.
29
+
30
+ # Calculates maxlength from length validations for string inputs
31
+ b.optional :maxlength
32
+
33
+ # Calculates pattern from format validations for string inputs
34
+ b.optional :pattern
35
+
36
+ # Calculates min and max from length validations for numeric inputs
37
+ b.optional :min_max
38
+
39
+ # Calculates readonly automatically from readonly attributes
40
+ b.optional :readonly
41
+
42
+ ## Inputs
43
+ b.use :label_input
44
+ b.use :hint, :wrap_with => { :tag => :span, :class => :hint }
45
+ b.use :error, :wrap_with => { :tag => :span, :class => :error }
46
+ end
47
+
48
+ # The default wrapper to be used by the FormBuilder.
49
+ config.default_wrapper = :default
50
+
51
+ # Define the way to render check boxes / radio buttons with labels.
52
+ # Defaults to :nested for bootstrap config.
53
+ # :inline => input + label
54
+ # :nested => label > input
55
+ config.boolean_style = :nested
56
+
57
+ # Default class for buttons
58
+ config.button_class = 'btn'
59
+
60
+ # Method used to tidy up errors. Specify any Rails Array method.
61
+ # :first lists the first message for each field.
62
+ # Use :to_sentence to list all errors for each field.
63
+ # config.error_method = :first
64
+
65
+ # Default tag used for error notification helper.
66
+ config.error_notification_tag = :div
67
+
68
+ # CSS class to add for error notification helper.
69
+ config.error_notification_class = 'alert alert-error'
70
+
71
+ # ID to add for error notification helper.
72
+ # config.error_notification_id = nil
73
+
74
+ # Series of attempts to detect a default label method for collection.
75
+ # config.collection_label_methods = [ :to_label, :name, :title, :to_s ]
76
+
77
+ # Series of attempts to detect a default value method for collection.
78
+ # config.collection_value_methods = [ :id, :to_s ]
79
+
80
+ # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none.
81
+ # config.collection_wrapper_tag = nil
82
+
83
+ # You can define the class to use on all collection wrappers. Defaulting to none.
84
+ # config.collection_wrapper_class = nil
85
+
86
+ # You can wrap each item in a collection of radio/check boxes with a tag,
87
+ # defaulting to :span. Please note that when using :boolean_style = :nested,
88
+ # SimpleForm will force this option to be a label.
89
+ # config.item_wrapper_tag = :span
90
+
91
+ # You can define a class to use in all item wrappers. Defaulting to none.
92
+ # config.item_wrapper_class = nil
93
+
94
+ # How the label text should be generated altogether with the required text.
95
+ # config.label_text = lambda { |label, required| "#{required} #{label}" }
96
+
97
+ # You can define the class to use on all labels. Default is nil.
98
+ config.label_class = 'control-label'
99
+
100
+ # You can define the class to use on all forms. Default is simple_form.
101
+ # config.form_class = :simple_form
102
+
103
+ # You can define which elements should obtain additional classes
104
+ # config.generate_additional_classes_for = [:wrapper, :label, :input]
105
+
106
+ # Whether attributes are required by default (or not). Default is true.
107
+ # config.required_by_default = true
108
+
109
+ # Tell browsers whether to use default HTML5 validations (novalidate option).
110
+ # Default is enabled.
111
+ config.browser_validations = false
112
+
113
+ # Collection of methods to detect if a file type was given.
114
+ # config.file_methods = [ :mounted_as, :file?, :public_filename ]
115
+
116
+ # Custom mappings for input types. This should be a hash containing a regexp
117
+ # to match as key, and the input type that will be used when the field name
118
+ # matches the regexp as value.
119
+ # config.input_mappings = { /count/ => :integer }
120
+
121
+ # Default priority for time_zone inputs.
122
+ # config.time_zone_priority = nil
123
+
124
+ # Default priority for country inputs.
125
+ # config.country_priority = nil
126
+
127
+ # Default size for text inputs.
128
+ # config.default_input_size = 50
129
+
130
+ # When false, do not use translations for labels.
131
+ # config.translate_labels = true
132
+
133
+ # Automatically discover new inputs in Rails' autoload path.
134
+ # config.inputs_discovery = true
135
+
136
+ # Cache SimpleForm inputs discovery
137
+ # config.cache_discovery = !Rails.env.development?
138
+ end
@@ -0,0 +1,14 @@
1
+ # Be sure to restart your server when you modify this file.
2
+ #
3
+ # This file contains settings for ActionController::ParamsWrapper which
4
+ # is enabled by default.
5
+
6
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
7
+ ActiveSupport.on_load(:action_controller) do
8
+ wrap_parameters format: [:json]
9
+ end
10
+
11
+ # Disable root element in JSON by default.
12
+ ActiveSupport.on_load(:active_record) do
13
+ self.include_root_in_json = false
14
+ end
@@ -0,0 +1,8 @@
1
+ Example::Application.routes.draw do
2
+ # The priority is based upon order of creation:
3
+ # first created -> highest priority.
4
+
5
+ resource :form
6
+
7
+ root to: redirect('/form/')
8
+ end
data/example/config.ru ADDED
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Example::Application
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/404.html -->
21
+ <div class="dialog">
22
+ <h1>The page you were looking for doesn't exist.</h1>
23
+ <p>You may have mistyped the address or the page may have moved.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,26 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/422.html -->
21
+ <div class="dialog">
22
+ <h1>The change you wanted was rejected.</h1>
23
+ <p>Maybe you tried to change something you didn't have access to.</p>
24
+ </div>
25
+ </body>
26
+ </html>
@@ -0,0 +1,25 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <style type="text/css">
6
+ body { background-color: #fff; color: #666; text-align: center; font-family: arial, sans-serif; }
7
+ div.dialog {
8
+ width: 25em;
9
+ padding: 0 4em;
10
+ margin: 4em auto 0 auto;
11
+ border: 1px solid #ccc;
12
+ border-right-color: #999;
13
+ border-bottom-color: #999;
14
+ }
15
+ h1 { font-size: 100%; color: #f00; line-height: 1.5em; }
16
+ </style>
17
+ </head>
18
+
19
+ <body>
20
+ <!-- This file lives in public/500.html -->
21
+ <div class="dialog">
22
+ <h1>We're sorry, but something went wrong.</h1>
23
+ </div>
24
+ </body>
25
+ </html>
File without changes
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,107 @@
1
+ module ActiveModel
2
+ class Form
3
+ module BooleanAttribute
4
+ def self.parse(v)
5
+ if ['1', 't', 'true', 'yes'].include? v
6
+ true
7
+ elsif ['0', 'f', 'false', 'no'].include? v
8
+ false
9
+ elsif ['', nil].include? v
10
+ nil
11
+ else
12
+ raise "Couldn't parse boolean attribute value: '#{v}'"
13
+ end
14
+ end
15
+
16
+ def self.type
17
+ :boolean
18
+ end
19
+
20
+ def self.number?
21
+ false
22
+ end
23
+ end
24
+
25
+ module IntegerAttribute
26
+ def self.parse(v)
27
+ if v.present?
28
+ Integer(v)
29
+ elsif ['', nil].include? v
30
+ nil
31
+ else
32
+ raise "Couldn't parse integer attribute value: '#{v}'"
33
+ end
34
+ end
35
+
36
+ def self.type
37
+ :integer
38
+ end
39
+
40
+ def self.number?
41
+ true
42
+ end
43
+ end
44
+
45
+ module FloatAttribute
46
+ def self.parse(v)
47
+ if v.present?
48
+ Float(v)
49
+ elsif ['', nil].include? v
50
+ nil
51
+ else
52
+ raise "Couldn't parse float attribute value: '#{v}'"
53
+ end
54
+ end
55
+
56
+ def self.type
57
+ :float
58
+ end
59
+
60
+ def self.number?
61
+ true
62
+ end
63
+ end
64
+
65
+ module StringAttribute
66
+ def self.parse(v)
67
+ if v.present?
68
+ String.new(v)
69
+ elsif ['', nil].include? v
70
+ nil
71
+ else
72
+ raise "Couldn't parse string attribute value: '#{v}'"
73
+ end
74
+ end
75
+
76
+ def self.type
77
+ :string
78
+ end
79
+
80
+ def self.number?
81
+ false
82
+ end
83
+
84
+ def self.limit
85
+ nil
86
+ end
87
+ end
88
+
89
+ module DateTimeAttribute
90
+ def self.parse(v)
91
+ if v.compact.present?
92
+ DateTime.new(*v)
93
+ else
94
+ nil
95
+ end
96
+ end
97
+
98
+ def self.type
99
+ :timestamp
100
+ end
101
+
102
+ def self.number?
103
+ false
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ class Form
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,85 @@
1
+ require 'active_model'
2
+ require 'active_model/form/version'
3
+ require 'active_model/form/attributes'
4
+ require 'active_support/inflector'
5
+
6
+ module ActiveModel
7
+ class Form
8
+ include ActiveModel::Validations
9
+ include ActiveModel::Conversion
10
+ include ActiveModel
11
+ extend ActiveModel::Naming
12
+
13
+ def initialize(attributes=nil)
14
+ self.assign_attributes(attributes)
15
+ end
16
+
17
+ # For the Rails route helpers
18
+ def persisted?
19
+ false
20
+ end
21
+
22
+ # Set the model name to change the field names generated by the Rails form helpers
23
+ def self.model_name=(name)
24
+ @_model_name = ActiveModel::Name.new(self, nil, name)
25
+ end
26
+
27
+ def self.attribute(name, type_name)
28
+ # TODO: Make inheritance of ActiveModel::Forms do the right thing with attributes
29
+ @_attributes ||= {}
30
+ @_attributes[name.to_s] = self.type_from_type_name(type_name)
31
+ self.send(:attr_accessor, name)
32
+ end
33
+
34
+ def self.attributes
35
+ @_attributes ||= {}
36
+ end
37
+
38
+ # Returns the column object for the named attribute.
39
+ def column_for_attribute(name)
40
+ self.class.attributes[name.to_s]
41
+ end
42
+
43
+ def assign_attributes(new_attributes)
44
+ return if new_attributes.blank?
45
+ new_attributes = self.clean_attributes(new_attributes)
46
+ new_attributes.each do |k, v|
47
+ if attribute = self.class.attributes[k].presence
48
+ send("#{k}=", attribute.parse(v))
49
+ end
50
+ end
51
+ end
52
+
53
+ protected
54
+
55
+ def clean_attributes(attributes)
56
+ cleaned_attributes = {}
57
+
58
+ attributes.stringify_keys.each do |k, v|
59
+ if k.include?("(")
60
+ attribute_name = k.split("(").first
61
+ cleaned_attributes[attribute_name] ||= []
62
+ v = v.empty? ? nil : type_cast_attribute_value(k, v)
63
+ cleaned_attributes[attribute_name][find_parameter_position(k)] ||= v
64
+ else
65
+ cleaned_attributes[k] = v
66
+ end
67
+ end
68
+
69
+ cleaned_attributes
70
+ end
71
+
72
+ def self.type_from_type_name(type_name)
73
+ classified_type_name = ActiveSupport::Inflector.classify(type_name)
74
+ ActiveSupport::Inflector.safe_constantize("::ActiveModel::Form::#{classified_type_name}Attribute")
75
+ end
76
+
77
+ def type_cast_attribute_value(multiparameter_name, value)
78
+ multiparameter_name =~ /\([0-9]*([if])\)/ ? value.send("to_" + $1) : value
79
+ end
80
+
81
+ def find_parameter_position(multiparameter_name)
82
+ multiparameter_name.scan(/\(([0-9]*).*\)/).first.first.to_i - 1
83
+ end
84
+ end
85
+ end
@@ -0,0 +1 @@
1
+ require 'active_model/form'
@@ -0,0 +1,24 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe ActiveModel::Form do
4
+ # Boolean params generated by the Rails form helpers look like this
5
+ let(:params) do
6
+ { "form"=>
7
+ {"not_checked"=>"0",
8
+ "checked"=>"1"}
9
+ }
10
+ end
11
+
12
+ it "parses the Rails form helper boolean params" do
13
+ class FormWithBooleans < ActiveModel::Form
14
+ attribute :not_checked, :boolean
15
+ attribute :checked, :boolean
16
+ attribute :not_set, :boolean
17
+ end
18
+
19
+ form = FormWithBooleans.new(params['form'])
20
+ form.not_checked.must_equal false
21
+ form.checked.must_equal true
22
+ form.not_set.must_equal nil # TODO: Should this be nil or false?
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe ActiveModel::Form do
4
+ # DateTime params generated by the Rails form helpers look like this
5
+ let(:params) do
6
+ { "form"=>
7
+ {"created_at(1i)"=>"2012",
8
+ "created_at(2i)"=>"8",
9
+ "created_at(3i)"=>"2",
10
+ "created_at(4i)"=>"17",
11
+ "created_at(5i)"=>"21",
12
+ "whatever"=>"yeah"} }
13
+ end
14
+
15
+ it "parses the Rails form helper datetime params" do
16
+ class FormWithDateTime < ActiveModel::Form
17
+ attribute :created_at, :date_time
18
+ attribute :something_else, :string
19
+ attribute :whatever, :string
20
+ end
21
+
22
+ form = FormWithDateTime.new(params['form'])
23
+ form.created_at.must_equal(DateTime.new(2012, 8, 2, 17, 21))
24
+ form.something_else.must_equal nil
25
+ form.whatever.must_equal "yeah"
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe ActiveModel::Form do
4
+ # Integr params generated by the Rails form helpers look like this
5
+ let(:params) do
6
+ { "form"=>
7
+ {"zero"=>"0",
8
+ "one"=>"1"}
9
+ }
10
+ end
11
+
12
+ it "parses the Rails form helper boolean params" do
13
+ class FormWithBooleans < ActiveModel::Form
14
+ attribute :zero, :integer
15
+ attribute :one, :integer
16
+ attribute :not_set, :integer
17
+ end
18
+
19
+ form = FormWithBooleans.new(params['form'])
20
+ form.zero.must_equal 0
21
+ form.one.must_equal 1
22
+ form.not_set.must_equal nil
23
+ end
24
+ end
@@ -0,0 +1,17 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe ActiveModel::Form do
4
+ it "can be set but doesn't influence subclasses" do
5
+ class Parent < ActiveModel::Form
6
+ self.model_name = 'the_parent'
7
+ end
8
+ class Child < ActiveModel::Form
9
+ self.model_name = 'the_child'
10
+ end
11
+
12
+ ActiveModel::Form.model_name.must_equal 'ActiveModel::Form'
13
+ Parent.model_name.must_equal 'the_parent'
14
+ Child.model_name.must_equal 'the_child'
15
+ end
16
+ end
17
+
@@ -0,0 +1,6 @@
1
+ require 'minitest/spec'
2
+ require 'minitest/autorun'
3
+
4
+ lib = File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'activemodel-form'