simple_form_class 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/.gitignore +28 -0
  2. data/.travis.yml +19 -0
  3. data/Gemfile +27 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +31 -0
  6. data/Rakefile +45 -0
  7. data/lib/simple_form_class.rb +10 -0
  8. data/lib/simple_form_class/base.rb +212 -0
  9. data/lib/simple_form_class/version.rb +3 -0
  10. data/simple_form_class.gemspec +33 -0
  11. data/test/.gitkeep +0 -0
  12. data/test/dummy/.gitignore +15 -0
  13. data/test/dummy/Gemfile +38 -0
  14. data/test/dummy/README.rdoc +261 -0
  15. data/test/dummy/Rakefile +7 -0
  16. data/test/dummy/app/assets/images/rails.png +0 -0
  17. data/test/dummy/app/assets/javascripts/application.js +15 -0
  18. data/test/dummy/app/assets/javascripts/orders.js.coffee +3 -0
  19. data/test/dummy/app/assets/javascripts/products.js.coffee +3 -0
  20. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  21. data/test/dummy/app/assets/stylesheets/orders.css.scss +3 -0
  22. data/test/dummy/app/assets/stylesheets/products.css.scss +3 -0
  23. data/test/dummy/app/assets/stylesheets/scaffolds.css.scss +69 -0
  24. data/test/dummy/app/controllers/application_controller.rb +3 -0
  25. data/test/dummy/app/controllers/orders_controller.rb +83 -0
  26. data/test/dummy/app/controllers/products_controller.rb +83 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/helpers/orders_helper.rb +2 -0
  29. data/test/dummy/app/helpers/products_helper.rb +2 -0
  30. data/test/dummy/app/mailers/.gitkeep +0 -0
  31. data/test/dummy/app/models/.gitkeep +0 -0
  32. data/test/dummy/app/models/order.rb +2 -0
  33. data/test/dummy/app/models/product.rb +2 -0
  34. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  35. data/test/dummy/app/views/orders/_form.html.erb +29 -0
  36. data/test/dummy/app/views/orders/edit.html.erb +6 -0
  37. data/test/dummy/app/views/orders/index.html.erb +27 -0
  38. data/test/dummy/app/views/orders/new.html.erb +5 -0
  39. data/test/dummy/app/views/orders/show.html.erb +20 -0
  40. data/test/dummy/app/views/products/_form.html.erb +25 -0
  41. data/test/dummy/app/views/products/edit.html.erb +6 -0
  42. data/test/dummy/app/views/products/index.html.erb +25 -0
  43. data/test/dummy/app/views/products/new.html.erb +5 -0
  44. data/test/dummy/app/views/products/show.html.erb +15 -0
  45. data/test/dummy/config.ru +4 -0
  46. data/test/dummy/config/application.rb +64 -0
  47. data/test/dummy/config/boot.rb +6 -0
  48. data/test/dummy/config/database.yml +25 -0
  49. data/test/dummy/config/environment.rb +5 -0
  50. data/test/dummy/config/environments/development.rb +24 -0
  51. data/test/dummy/config/environments/production.rb +55 -0
  52. data/test/dummy/config/environments/test.rb +25 -0
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/dummy/config/initializers/inflections.rb +15 -0
  55. data/test/dummy/config/initializers/mime_types.rb +5 -0
  56. data/test/dummy/config/initializers/secret_token.rb +7 -0
  57. data/test/dummy/config/initializers/session_store.rb +8 -0
  58. data/test/dummy/config/locales/en.yml +5 -0
  59. data/test/dummy/config/routes.rb +64 -0
  60. data/test/dummy/db/migrate/20130420130732_create_products.rb +10 -0
  61. data/test/dummy/db/migrate/20130420130801_create_orders.rb +11 -0
  62. data/test/dummy/db/schema.rb +31 -0
  63. data/test/dummy/db/seeds.rb +7 -0
  64. data/test/dummy/lib/assets/.gitkeep +0 -0
  65. data/test/dummy/lib/tasks/.gitkeep +0 -0
  66. data/test/dummy/log/.gitkeep +0 -0
  67. data/test/dummy/public/404.html +26 -0
  68. data/test/dummy/public/422.html +26 -0
  69. data/test/dummy/public/500.html +25 -0
  70. data/test/dummy/public/favicon.ico +0 -0
  71. data/test/dummy/public/index.html +241 -0
  72. data/test/dummy/public/robots.txt +5 -0
  73. data/test/dummy/script/rails +6 -0
  74. data/test/dummy/test/fixtures/.gitkeep +0 -0
  75. data/test/dummy/test/fixtures/orders.yml +11 -0
  76. data/test/dummy/test/fixtures/products.yml +9 -0
  77. data/test/dummy/test/functional/.gitkeep +0 -0
  78. data/test/dummy/test/functional/orders_controller_test.rb +49 -0
  79. data/test/dummy/test/functional/products_controller_test.rb +49 -0
  80. data/test/dummy/test/integration/.gitkeep +0 -0
  81. data/test/dummy/test/performance/browsing_test.rb +12 -0
  82. data/test/dummy/test/test_helper.rb +13 -0
  83. data/test/dummy/test/unit/.gitkeep +0 -0
  84. data/test/dummy/test/unit/helpers/orders_helper_test.rb +4 -0
  85. data/test/dummy/test/unit/helpers/products_helper_test.rb +4 -0
  86. data/test/dummy/test/unit/order_test.rb +7 -0
  87. data/test/dummy/test/unit/product_test.rb +7 -0
  88. data/test/dummy/vendor/assets/javascripts/.gitkeep +0 -0
  89. data/test/dummy/vendor/assets/stylesheets/.gitkeep +0 -0
  90. data/test/dummy/vendor/plugins/.gitkeep +0 -0
  91. data/test/support/dummy_app.rb +86 -0
  92. data/test/test_helper.rb +66 -0
  93. data/test/unit/base_test.rb +210 -0
  94. metadata +405 -0
@@ -0,0 +1,28 @@
1
+ # bundler gem defaults
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
19
+
20
+ # dummy app
21
+ spec/dummy/log/*
22
+ spec/dummy/db/*.sqlite3
23
+
24
+ # rvmrc
25
+ *.rvmrc
26
+
27
+ # NetBeans project folder
28
+ nbproject
@@ -0,0 +1,19 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - rbx-19mode
7
+ - jruby-19mode
8
+ - ruby-head
9
+
10
+ env:
11
+ - "RAILS_VERSION=3.2"
12
+ - "RAILS_VERSION=3.1"
13
+ - "RAILS_VERSION=3.0"
14
+ - "RAILS_VERSION=master"
15
+
16
+ matrix:
17
+ allow_failures:
18
+ - rvm: ruby-head
19
+ - env: "RAILS_VERSION=master"
data/Gemfile ADDED
@@ -0,0 +1,27 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ platforms :ruby do
6
+ gem "sqlite3"
7
+ end
8
+
9
+ platforms :jruby do
10
+ gem "minitest", ">= 3.0"
11
+ gem "activerecord-jdbcsqlite3-adapter"
12
+ end
13
+
14
+ version = ENV["RAILS_VERSION"] || "3.2"
15
+
16
+ rails = case version
17
+ when "master"
18
+ {github: "rails/rails"}
19
+ else
20
+ "~> #{version}.0"
21
+ end
22
+
23
+ gem "rails", rails
24
+
25
+ gem 'minitest-colorize', :git => 'git://github.com/bbozo/minitest-colorize.git', :tag => 'v0.0.4.1'
26
+
27
+ gem 'strong_parameters'
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Borna Novak
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,31 @@
1
+ # SimpleFormClass
2
+
3
+ [![Build Status](https://travis-ci.org/bbozo/simple_form_class.png?branch=master)](https://travis-ci.org/bbozo/simple_form_class) [![Code Climate](https://codeclimate.com/github/bbozo/simple_form_class.png)](https://codeclimate.com/github/bbozo/simple_form_class)
4
+
5
+ TODO: Write a gem description
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'simple_form_class'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install simple_form_class
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
@@ -0,0 +1,45 @@
1
+ require 'rake'
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ def run_in_dummy_app(command)
6
+ success = system("cd test/dummy && #{command}")
7
+ raise "#{command} failed" unless success
8
+ end
9
+
10
+ task "default" => "ci"
11
+
12
+ desc "Run all tests for CI"
13
+ task "ci" => "test"
14
+
15
+ desc "Run all tests"
16
+ task "test" => "test:all"
17
+
18
+ namespace "test" do
19
+ task "all" => ["db:setup", "unit"]
20
+
21
+ def test_task(name)
22
+ desc "Run #{name} tests"
23
+ Rake::TestTask.new(name) do |t|
24
+ t.libs << "test"
25
+ t.pattern = "test/#{name}/**/*_test.rb"
26
+ end
27
+ end
28
+
29
+ test_task "unit"
30
+ end
31
+
32
+ namespace "db" do
33
+ desc "Set up databases for integration testing"
34
+ task "setup" do
35
+ puts "Setting up development, production & test databases in parallel"
36
+ run_in_dummy_app "rm -f db/*.sqlite3"
37
+
38
+ threads = []
39
+ threads << Thread.new{ run_in_dummy_app "RAILS_ENV=development rake db:schema:load db:seed" }
40
+ threads << Thread.new{ run_in_dummy_app "RAILS_ENV=production rake db:schema:load db:seed" }
41
+ threads << Thread.new{ run_in_dummy_app "RAILS_ENV=test rake db:schema:load" }
42
+
43
+ threads.each(&:join)
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ require "simple_form_class/version"
2
+
3
+ require "action_controller/base"
4
+ require "simple_form_class/base"
5
+
6
+ module SimpleFormClass
7
+
8
+ class InvalidOwner < ArgumentError; end
9
+
10
+ end
@@ -0,0 +1,212 @@
1
+ module SimpleFormClass
2
+ class Base
3
+
4
+ MANDATORY_OWNER_METHODS = [ :attributes, :attributes=, :valid?, :save ]
5
+
6
+ extend ActiveModel::Callbacks
7
+ extend ActiveModel::Naming
8
+ include ActiveModel::Conversion
9
+ include ActiveModel::Validations
10
+ include ActiveModel::Serialization # TODO: remove?
11
+
12
+ define_model_callbacks :save, :initialize, :validation
13
+
14
+ attr_accessor :params
15
+ alias_method :attributes, :params
16
+
17
+ attr_accessor :options
18
+
19
+
20
+ def initialize(params = nil, options = {})
21
+ run_callbacks :initialize do
22
+ @params = params || {}
23
+ @options = options
24
+
25
+ yield self if block_given?
26
+
27
+ check_if_sane_owners!
28
+ self.attributes = @params
29
+ end
30
+ end
31
+
32
+ #yes, this method is needed, form breaks without it
33
+ #check why later
34
+ def persisted?
35
+ false
36
+ end
37
+
38
+ validate :owners_must_be_valid
39
+
40
+ def self.field field_name, options = {}
41
+ add_owner options[:owner]
42
+
43
+ @fields ||= {}
44
+ @fields[field_name] = options
45
+
46
+ if options[:owner] == :self
47
+ attr_accessor field_name
48
+ else
49
+ delegate field_name, "#{field_name}=", :to => options[:owner], :null => false
50
+ end
51
+ end
52
+
53
+
54
+ def self.owners
55
+ @owners ||= []
56
+ @owners = @owners + superclass.owners if superclass.respond_to? :owners
57
+ @owners.uniq
58
+ end
59
+
60
+ def self.fields
61
+ @fields ||= {}
62
+ @fields.merge! superclass.fields if superclass.respond_to? :fields
63
+ @fields
64
+ end
65
+
66
+ def options
67
+ @options || {}
68
+ end
69
+
70
+ def owners(*args)
71
+ owner_hash(*args).values.compact
72
+ end
73
+
74
+ def self.fields_for_owner owner
75
+ fields.reject{|k,v| not v[:owner] == owner}.keys
76
+ end
77
+
78
+ def self.permitted_fields_for_owner owner
79
+ fields.reject{|k,v| not (v[:owner] == owner and v[:write])}.keys
80
+ end
81
+
82
+ def save(*args)
83
+ validate = options.has_key?(:validate) ? options[:validate] : true
84
+
85
+ if validate
86
+ return false unless valid?
87
+ end
88
+
89
+ ActiveRecord::Base.transaction do
90
+ run_callbacks :save do
91
+ not owners(except_self: true).map{ |owner| owner.save(*args) }.include?(false)
92
+ end
93
+ end
94
+ end
95
+
96
+ def save!
97
+ save || raise(ActiveRecord::RecordInvalid.new(self))
98
+ end
99
+
100
+ def self.add_owner owner
101
+ @owners ||= []
102
+
103
+ unless @owners.include? owner
104
+ @owners << owner
105
+
106
+ attr_accessor owner
107
+ end
108
+ end
109
+
110
+
111
+ private
112
+
113
+
114
+ def owners_must_be_valid
115
+ self.class.owners.each do |owner_sym|
116
+ next if owner_sym == :self
117
+
118
+ owner = send owner_sym
119
+ unless owner.valid?
120
+ errors.add(:base, "#{owner_sym} of class #{owner.class.to_s} is invalid")
121
+ delegate_owner_error_messages_to_self owner
122
+ end
123
+ end
124
+ end
125
+
126
+ def delegate_owner_error_messages_to_self owner
127
+ owner.errors.messages.each do |attribute, messages|
128
+ next unless self.class.fields.has_key? attribute
129
+
130
+ messages.each do |message|
131
+ errors.add(attribute, message)
132
+ end
133
+ end
134
+ end
135
+
136
+ def attributes=(attributes)
137
+ self.class.owners.each do |owner|
138
+ owners_hash = attributes_for_owner owner, attributes
139
+ owners_attribute_setter = owner == :self ? :private_attributes= : :attributes=
140
+
141
+ get_owner(owner).send(
142
+ owners_attribute_setter,
143
+ if defined?(ActionController::Parameters) && owners_hash.is_a?(ActionController::Parameters)
144
+ owners_hash.permit(
145
+ *self.class.permitted_fields_for_owner(owner)
146
+ )
147
+ else
148
+ owners_hash
149
+ end
150
+ )
151
+ end
152
+ end
153
+
154
+
155
+ def attributes_for_owner owner, attributes = attributes
156
+ attributes.slice(*self.class.fields_for_owner(owner))
157
+ end
158
+
159
+ def get_owner(owner)
160
+ if owner == :self
161
+ self
162
+ else
163
+ send(owner)
164
+ end
165
+ end
166
+
167
+ def private_attributes=(attributes)
168
+ attributes.each do |k,v|
169
+ send("#{k}=", v)
170
+ end
171
+ end
172
+
173
+ def owner_hash(options = {})
174
+ # options[:except_self]
175
+
176
+ Hash[
177
+ self.class.owners.map do |owner_sym|
178
+ [
179
+ owner_sym,
180
+ if owner_sym == :self and options[:except_self]
181
+ nil
182
+ else
183
+ get_owner owner_sym
184
+ end
185
+ ]
186
+ end
187
+ ]
188
+ end
189
+
190
+ def check_if_sane_owners!
191
+ owner_hash.each do |owner_sym, owner|
192
+ missing_expected_method = MANDATORY_OWNER_METHODS.detect{ |method| not owner.respond_to? method, true }
193
+
194
+ if missing_expected_method
195
+ raise InvalidOwner, "owner '#{owner_sym}' thas is of class #{owner.class} should behave like an ActiveModel object, but it doesn't respond to ##{missing_expected_method}"
196
+ end
197
+ end
198
+ end
199
+
200
+ protected
201
+
202
+ def run_validations_with_validation_callback!(*args, &block)
203
+ run_callbacks :validation do
204
+ run_validations_without_validation_callback!(*args, &block)
205
+ end
206
+ errors.empty?
207
+ end
208
+ alias_method_chain :run_validations!, :validation_callback
209
+
210
+
211
+ end
212
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleFormClass
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_form_class/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "simple_form_class"
8
+ spec.version = SimpleFormClass::VERSION
9
+ spec.authors = ["Borna Novak"]
10
+ spec.email = ["dosadnizub@gmail.com"]
11
+ spec.description = %q{An implementation of the form class pattern, for controller use}
12
+ spec.summary = %q{Rails model validators when used in forms are a clear break of MVC architecture and strong_parameters make things unDRY, this is one take on making things be Better}
13
+ spec.homepage = "https://github.com/bbozo/simple_form_class"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activemodel', '>= 3.0'
22
+ spec.add_dependency 'activerecord', '>= 3.0'
23
+ spec.add_dependency 'actionpack', '>= 3.0'
24
+ spec.add_dependency 'strong_parameters'
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-rails"
30
+ spec.add_development_dependency "pry-nav"
31
+ spec.add_development_dependency 'minitest', '=4.5.0'
32
+ spec.add_development_dependency 'minitest-rails-shoulda'
33
+ end
File without changes
@@ -0,0 +1,15 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile ~/.gitignore_global
6
+
7
+ # Ignore bundler config
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+
13
+ # Ignore all logfiles and tempfiles.
14
+ /log/*.log
15
+ /tmp