form_forms 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,16 @@
1
+ require 'test_helper'
2
+
3
+ module FormForms
4
+ class FormRegistryTest < ActionView::TestCase
5
+ test "set, get, and delete registered form" do
6
+ assert_nil FormRegistry['foo']
7
+
8
+ form = Forms::Form.new{}
9
+ FormRegistry['foo'] = form
10
+ assert_equal FormRegistry['foo'], form
11
+
12
+ FormRegistry.delete('foo')
13
+ assert_nil FormRegistry['foo']
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ module FormForms
4
+ module Forms
5
+ class FormTest < ActionView::TestCase
6
+ test "register form" do
7
+ assert_nil FormForms::FormRegistry['form_test']
8
+
9
+ form = Form.register('form_test'){}
10
+
11
+ assert_equal FormForms::FormRegistry['form_test'], form
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,15 @@
1
+ module MiscHelpers
2
+ def assert_no_select(selector, value = nil)
3
+ assert_select(selector, :text => value, :count => 0)
4
+ end
5
+
6
+ def with_form_for(object, *args, &block)
7
+ form = FormForms::Forms::Form.new(*args, &(block || proc {}))
8
+ @rendered = form.render(object, self)
9
+ concat @rendered
10
+ end
11
+
12
+ def rendered
13
+ @rendered
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ class MockController
2
+ attr_writer :action_name
3
+
4
+ def _routes
5
+ self
6
+ end
7
+
8
+ def action_name
9
+ defined?(@action_name) ? @action_name : "edit"
10
+ end
11
+
12
+ def url_for(*args)
13
+ "http://example.com"
14
+ end
15
+
16
+ def url_helpers
17
+ self
18
+ end
19
+
20
+ def hash_for_user_path(*args); end
21
+ def hash_for_users_path(*args); end
22
+ end
@@ -0,0 +1,134 @@
1
+ # Most of of this is "stolen" from simple_form
2
+
3
+ # Always use the same parent, even after multiple requires
4
+ ColumnParent = Struct.new(:name, :type, :limit) unless defined?(ColumnParent)
5
+ class Column < ColumnParent
6
+ def number?
7
+ type == :integer
8
+ end
9
+ end
10
+
11
+ Association = Struct.new(:klass, :name, :macro, :options)
12
+
13
+ # Always use the same parent, even after multiple requires
14
+ CompanyParent = Struct.new(:id, :name) unless defined?(CompanyParent)
15
+ class Company < CompanyParent
16
+ extend ActiveModel::Naming
17
+ include ActiveModel::Conversion
18
+
19
+ def self.all(options={})
20
+ all = (1..3).map{|i| Company.new(i, "Company #{i}")}
21
+ return [all.first] if options[:conditions].present?
22
+ return [all.last] if options[:order].present?
23
+ return all[0..1] if options[:include].present?
24
+ return all[1..2] if options[:joins].present?
25
+ all
26
+ end
27
+
28
+ def self.merge_conditions(a, b)
29
+ (a || {}).merge(b || {})
30
+ end
31
+
32
+ def persisted?
33
+ true
34
+ end
35
+ end
36
+
37
+ class Tag < Company
38
+ def self.all(options={})
39
+ (1..3).map{|i| Tag.new(i, "Tag #{i}")}
40
+ end
41
+ end
42
+
43
+ class User
44
+ extend ActiveModel::Naming
45
+ include ActiveModel::Conversion
46
+
47
+ attr_accessor :id, :name, :company, :company_id, :time_zone, :active, :age,
48
+ :description, :created_at, :updated_at, :credit_limit, :password, :url,
49
+ :delivery_time, :born_at, :special_company_id, :country, :tags, :tag_ids,
50
+ :avatar, :home_picture, :email, :status, :residence_country, :phone_number,
51
+ :post_count, :lock_version, :amount, :attempts, :action, :credit_card, :gender
52
+
53
+ def initialize(options={})
54
+ @new_record = false
55
+ options.each do |key, value|
56
+ send("#{key}=", value)
57
+ end if options
58
+ end
59
+
60
+ def new_record!
61
+ @new_record = true
62
+ end
63
+
64
+ def persisted?
65
+ !@new_record
66
+ end
67
+
68
+ def company_attributes=(*)
69
+ end
70
+
71
+ def tags_attributes=(*)
72
+ end
73
+
74
+ def column_for_attribute(attribute)
75
+ column_type, limit = case attribute.to_sym
76
+ when :name, :status, :password then [:string, 100]
77
+ when :description then [:text, 200]
78
+ when :age then :integer
79
+ when :credit_limit then [:decimal, 15]
80
+ when :active then :boolean
81
+ when :born_at then :date
82
+ when :delivery_time then :time
83
+ when :created_at then :datetime
84
+ when :updated_at then :timestamp
85
+ when :lock_version then :integer
86
+ when :home_picture then :string
87
+ when :amount then :integer
88
+ when :attempts then :integer
89
+ when :action then :string
90
+ when :credit_card then :string
91
+ end
92
+ Column.new(attribute, column_type, limit)
93
+ end
94
+
95
+ def self.human_attribute_name(attribute)
96
+ case attribute
97
+ when 'name'
98
+ 'Super User Name!'
99
+ when 'description'
100
+ 'User Description!'
101
+ when 'company'
102
+ 'Company Human Name!'
103
+ else
104
+ attribute.humanize
105
+ end
106
+ end
107
+
108
+ def self.reflect_on_association(association)
109
+ case association
110
+ when :company
111
+ Association.new(Company, association, :belongs_to, {})
112
+ when :tags
113
+ Association.new(Tag, association, :has_many, {})
114
+ end
115
+ end
116
+
117
+ def errors
118
+ @errors ||= begin
119
+ hash = Hash.new { |h,k| h[k] = [] }
120
+ hash.merge!(
121
+ :name => ["can't be blank"],
122
+ :description => ["must be longer than 15 characters"],
123
+ :age => ["is not a number", "must be greater than 18"],
124
+ :company => ["company must be present"],
125
+ :company_id => ["must be valid"]
126
+ )
127
+ end
128
+ end
129
+
130
+ def self.readonly_attributes
131
+ [:credit_card]
132
+ end
133
+ end
134
+
@@ -0,0 +1,51 @@
1
+ require 'bundler'
2
+ Bundler.setup
3
+
4
+ $:.unshift File.expand_path("../../lib", __FILE__)
5
+ require 'form_forms'
6
+ require 'simple_form'
7
+
8
+ require 'test/unit'
9
+
10
+ require 'active_model'
11
+ require 'action_controller'
12
+ require 'action_view'
13
+ require 'action_view/template'
14
+
15
+ require 'active_support/core_ext/module/deprecation'
16
+ require 'action_view/test_case'
17
+
18
+ Dir["#{File.dirname(__FILE__)}/support/*.rb"].each do |file|
19
+ require file
20
+ end
21
+ I18n.default_locale = :en
22
+
23
+ class ActionView::TestCase
24
+ include SimpleForm::ActionViewExtensions::FormHelper
25
+ include MiscHelpers
26
+
27
+ setup :set_controller
28
+ setup :setup_new_user
29
+
30
+ def set_controller
31
+ @controller = MockController.new
32
+ end
33
+
34
+ def setup_new_user(options={})
35
+ @user = User.new({
36
+ :id => 1,
37
+ :name => 'New in SimpleForm!',
38
+ :description => 'Hello!',
39
+ :created_at => Time.now
40
+ }.merge(options))
41
+ end
42
+
43
+ def protect_against_forgery?
44
+ false
45
+ end
46
+
47
+ def user_path(*args)
48
+ '/users'
49
+ end
50
+ alias :users_path :user_path
51
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: form_forms
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Holger Just
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &2163535820 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2163535820
25
+ - !ruby/object:Gem::Dependency
26
+ name: actionpack
27
+ requirement: &2163535320 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2163535320
36
+ - !ruby/object:Gem::Dependency
37
+ name: simple_form
38
+ requirement: &2163534940 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *2163534940
47
+ - !ruby/object:Gem::Dependency
48
+ name: activemodel
49
+ requirement: &2163534400 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *2163534400
58
+ description: Configurable forms for Rails
59
+ email:
60
+ - hjust@meine-er.de
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - .gitignore
66
+ - .travis.yml
67
+ - Gemfile
68
+ - LICENSE
69
+ - README.md
70
+ - Rakefile
71
+ - form_forms.gemspec
72
+ - gemfiles/Gemfile.rails3_0
73
+ - gemfiles/Gemfile.rails3_1
74
+ - gemfiles/Gemfile.rails3_2
75
+ - lib/form_forms.rb
76
+ - lib/form_forms/elements.rb
77
+ - lib/form_forms/elements/base_element.rb
78
+ - lib/form_forms/elements/block.rb
79
+ - lib/form_forms/elements/field.rb
80
+ - lib/form_forms/elements/fields.rb
81
+ - lib/form_forms/elements/fieldset.rb
82
+ - lib/form_forms/form_registry.rb
83
+ - lib/form_forms/forms.rb
84
+ - lib/form_forms/forms/base_form.rb
85
+ - lib/form_forms/forms/form.rb
86
+ - lib/form_forms/version.rb
87
+ - test/form_forms/elements/block_test.rb
88
+ - test/form_forms/elements/field_test.rb
89
+ - test/form_forms/elements/fields_test.rb
90
+ - test/form_forms/elements/fieldset_test.rb
91
+ - test/form_forms/form_registry_test.rb
92
+ - test/form_forms/forms/form_test.rb
93
+ - test/support/misc_helpers.rb
94
+ - test/support/mock_controller.rb
95
+ - test/support/models.rb
96
+ - test/test_helper.rb
97
+ homepage: https://github.com/meineerde/form_forms
98
+ licenses: []
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ! '>='
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ none: false
111
+ requirements:
112
+ - - ! '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 1.8.17
118
+ signing_key:
119
+ specification_version: 3
120
+ summary: Configurable forms for Rails
121
+ test_files:
122
+ - test/form_forms/elements/block_test.rb
123
+ - test/form_forms/elements/field_test.rb
124
+ - test/form_forms/elements/fields_test.rb
125
+ - test/form_forms/elements/fieldset_test.rb
126
+ - test/form_forms/form_registry_test.rb
127
+ - test/form_forms/forms/form_test.rb
128
+ - test/support/misc_helpers.rb
129
+ - test/support/mock_controller.rb
130
+ - test/support/models.rb
131
+ - test/test_helper.rb