activeform-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (74) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +32 -0
  4. data/lib/activeform.rb +6 -0
  5. data/lib/activeform/form.rb +93 -0
  6. data/lib/activeform/validate_uniqueness.rb +44 -0
  7. data/lib/activeform/version.rb +3 -0
  8. data/lib/tasks/activeform_tasks.rake +4 -0
  9. data/spec/activeform/form_spec.rb +124 -0
  10. data/spec/activeform/validate_uniqueness_spec.rb +37 -0
  11. data/spec/dummy/Gemfile +7 -0
  12. data/spec/dummy/Gemfile.lock +94 -0
  13. data/spec/dummy/README.rdoc +28 -0
  14. data/spec/dummy/Rakefile +6 -0
  15. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  16. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  17. data/spec/dummy/app/assets/stylesheets/scaffold.css +56 -0
  18. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  19. data/spec/dummy/app/controllers/categories_controller.rb +64 -0
  20. data/spec/dummy/app/controllers/users_controller.rb +66 -0
  21. data/spec/dummy/app/forms/category_form.rb +24 -0
  22. data/spec/dummy/app/forms/user_form.rb +12 -0
  23. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  24. data/spec/dummy/app/models/category.rb +3 -0
  25. data/spec/dummy/app/models/user.rb +3 -0
  26. data/spec/dummy/app/views/categories/_form.html.erb +5 -0
  27. data/spec/dummy/app/views/categories/edit.html.erb +6 -0
  28. data/spec/dummy/app/views/categories/index.html.erb +27 -0
  29. data/spec/dummy/app/views/categories/new.html.erb +5 -0
  30. data/spec/dummy/app/views/categories/show.html.erb +9 -0
  31. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  32. data/spec/dummy/app/views/users/_form.html.erb +5 -0
  33. data/spec/dummy/app/views/users/edit.html.erb +6 -0
  34. data/spec/dummy/app/views/users/index.html.erb +27 -0
  35. data/spec/dummy/app/views/users/new.html.erb +5 -0
  36. data/spec/dummy/app/views/users/show.html.erb +9 -0
  37. data/spec/dummy/bin/bundle +3 -0
  38. data/spec/dummy/bin/rails +4 -0
  39. data/spec/dummy/bin/rake +4 -0
  40. data/spec/dummy/config.ru +4 -0
  41. data/spec/dummy/config/application.rb +23 -0
  42. data/spec/dummy/config/boot.rb +5 -0
  43. data/spec/dummy/config/database.yml +25 -0
  44. data/spec/dummy/config/environment.rb +5 -0
  45. data/spec/dummy/config/environments/development.rb +29 -0
  46. data/spec/dummy/config/environments/production.rb +80 -0
  47. data/spec/dummy/config/environments/test.rb +36 -0
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  50. data/spec/dummy/config/initializers/inflections.rb +16 -0
  51. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  52. data/spec/dummy/config/initializers/secret_token.rb +12 -0
  53. data/spec/dummy/config/initializers/session_store.rb +3 -0
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  55. data/spec/dummy/config/locales/en.yml +23 -0
  56. data/spec/dummy/config/routes.rb +61 -0
  57. data/spec/dummy/db/development.sqlite3 +0 -0
  58. data/spec/dummy/db/migrate/20140216201540_create_users.rb +10 -0
  59. data/spec/dummy/db/migrate/20140222122256_create_categories.rb +9 -0
  60. data/spec/dummy/db/schema.rb +31 -0
  61. data/spec/dummy/public/404.html +58 -0
  62. data/spec/dummy/public/422.html +58 -0
  63. data/spec/dummy/public/500.html +57 -0
  64. data/spec/dummy/public/favicon.ico +0 -0
  65. data/spec/dummy/test/controllers/categories_controller_test.rb +49 -0
  66. data/spec/dummy/test/controllers/users_controller_test.rb +49 -0
  67. data/spec/dummy/test/fixtures/categories.yml +7 -0
  68. data/spec/dummy/test/fixtures/users.yml +7 -0
  69. data/spec/dummy/test/helpers/categories_helper_test.rb +4 -0
  70. data/spec/dummy/test/helpers/users_helper_test.rb +4 -0
  71. data/spec/dummy/test/models/category_test.rb +7 -0
  72. data/spec/dummy/test/models/user_test.rb +7 -0
  73. data/spec/spec_helper.rb +32 -0
  74. metadata +223 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1377ce8c19ddd48e5ca4bafb74b4520f77216d21
4
+ data.tar.gz: 9afc807092665b8e45e684ea571ea67b406cbc42
5
+ SHA512:
6
+ metadata.gz: eb6be6b417983b2e09688e1b176af7e65a634acf3564554853f4d45b924b280cf21afb458434b985bcfeee83849eac0b9b1f453baff89f60486b4e3cb7f78e3b
7
+ data.tar.gz: 4c1c89e3fc6b5e1366d25f65245fd9fc492bfce0ce84f439d7c6c5417e5825cc6010c9261d3641773c7d95da84934acd03821219ec864d580ea4b49b4bcc6c84
@@ -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,32 @@
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 = 'Activeform'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
22
+ require 'rake/testtask'
23
+
24
+ Rake::TestTask.new(:test) do |t|
25
+ t.libs << 'lib'
26
+ t.libs << 'test'
27
+ t.pattern = 'test/**/*_test.rb'
28
+ t.verbose = false
29
+ end
30
+
31
+
32
+ task default: :test
@@ -0,0 +1,6 @@
1
+ module ActiveForm
2
+ end
3
+
4
+ require 'active_record'
5
+ require 'activeform/form'
6
+ require 'activeform/validate_uniqueness'
@@ -0,0 +1,93 @@
1
+ module ActiveForm::Form
2
+ def self.included(base)
3
+ base.class_eval do
4
+ extend ActiveModel::Naming
5
+ include ActiveModel::Conversion
6
+ include ActiveModel::Validations
7
+ end
8
+ base.extend ClassMethods
9
+ end
10
+
11
+
12
+ module ClassMethods
13
+ delegate :model_name, :reflect_on_association, to: :main_class
14
+ attr_accessor :main_class, :reflected_class, :main_model
15
+
16
+ def properties(*attributes, prefix: false, on:)
17
+ assign_delegators(attributes, on, prefix)
18
+ add_model_on_list(on)
19
+ add_accessor(on)
20
+ end
21
+
22
+ def i18n_scope
23
+ :activerecord
24
+ end
25
+
26
+ def models
27
+ @models ||= []
28
+ end
29
+
30
+ def main_class
31
+ @main_class ||= @main_model.to_s.camelize.constantize
32
+ end
33
+
34
+ private
35
+
36
+ def add_model_on_list(model_name)
37
+ models << model_name unless models.include?(model_name)
38
+ end
39
+
40
+ def add_accessor(model_name)
41
+ attr_accessor model_name
42
+ end
43
+
44
+ def assign_delegators(attributes, model_name, prefix)
45
+ attributes.each do |attribute|
46
+ delegate attribute, to: model_name, prefix: prefix
47
+ delegate "#{attribute}=", to: model_name, prefix: prefix
48
+ end
49
+ end
50
+ end
51
+
52
+ delegate :to_key, :to_param, :id, :persisted?, to: :main_model
53
+
54
+ def initialize(attributes)
55
+ assign_from_hash(attributes)
56
+ end
57
+
58
+ def fill_attributes(params)
59
+ assign_from_hash(params)
60
+ end
61
+
62
+ def save(&block)
63
+ valid?.tap do
64
+ call_action_or_block(:save, &block)
65
+ end
66
+ end
67
+
68
+ def save!(&block)
69
+ ActiveRecord::Base.transaction do
70
+ call_action_or_block(:save!, &block)
71
+ end
72
+ end
73
+
74
+ def main_model
75
+ send(self.class.main_model)
76
+ end
77
+
78
+ private
79
+
80
+ def each_models
81
+ self.class.models.each do |model_name|
82
+ yield(send(model_name))
83
+ end
84
+ end
85
+
86
+ def assign_from_hash(hash)
87
+ hash.each { |key, value| send("#{key}=", value) }
88
+ end
89
+
90
+ def call_action_or_block(action, &block)
91
+ block_given? ? block.call(self) : each_models(&action)
92
+ end
93
+ end
@@ -0,0 +1,44 @@
1
+ module ActiveForm
2
+ module ValidateUniqueness
3
+ def self.included(base)
4
+ base.class_eval do
5
+ extend ClassMethods
6
+ end
7
+ end
8
+
9
+ module ClassMethods
10
+ def validates_uniqueness_of(attribute, model_name)
11
+ @attribute = attribute
12
+ validates_each attribute do |form, attr, value|
13
+ @form = form
14
+ @model = form.send(model_name)
15
+ @klass = @model.class
16
+ @hash = { attribute => value }
17
+ add_error_message if another_model?
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def another_model?
24
+ @model.persisted? ? another_model_without_itself : any_model?
25
+ end
26
+
27
+ def another_model_without_itself
28
+ @klass.where(@hash).delete_if { |m| m.id == @model.id }.count >= 1
29
+ end
30
+
31
+ def any_model?
32
+ @klass.exists?(@hash)
33
+ end
34
+
35
+ def error_message
36
+ I18n.t('activerecord.errors.messages.exclusion')
37
+ end
38
+
39
+ def add_error_message
40
+ @form.errors.add(@attribute, error_message)
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Activeform
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :activeform do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,124 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveForm do
4
+ class Form
5
+ include ActiveForm::Form
6
+ properties :name, on: :user
7
+ properties :title, on: :category
8
+
9
+ attr_accessor :user, :category
10
+
11
+ self.main_model = :user
12
+ end
13
+
14
+ describe ".main_class" do
15
+ it "reflect assoctions of the main model" do
16
+ user = User.new
17
+ category = Category.new
18
+ user.categories = [category]
19
+ expect(Form.reflect_on_association(:has_many)).to eq \
20
+ User.reflect_on_association(:has_many)
21
+ end
22
+
23
+ context "when a main class is specified" do
24
+ it "take the specified class" do
25
+ Form.main_class = Category
26
+ expect(Form.main_class).to eq Category
27
+ end
28
+ end
29
+
30
+ context "when there is only a main model specified" do
31
+ it "build the main class from the main model" do
32
+ Form.main_model = :category
33
+ expect(Form.main_class).to eq Category
34
+ end
35
+ end
36
+ end
37
+
38
+ describe "#fill_attributes" do
39
+ it "assign variable to models" do
40
+ user = User.new
41
+ category = Category.new
42
+ form = Form.new(user: user, category: category)
43
+ form.fill_attributes(name: "Martin")
44
+ expect(user.name).to eq "Martin"
45
+ end
46
+ end
47
+
48
+ describe "#save" do
49
+ context "when the form is valid" do
50
+ context "when no block is given" do
51
+ it "save all models" do
52
+ user = User.new
53
+ category = Category.new
54
+ form = Form.new(user: user, category: category)
55
+ expect(user).to receive(:save)
56
+ expect(category).to receive(:save)
57
+ form.save
58
+ end
59
+
60
+ it "return true" do
61
+ form = Form.new(user: User.new, category: Category.new)
62
+ allow(form).to receive(:valid?).and_return(true)
63
+ expect(form.save).to eq true
64
+ end
65
+ end
66
+
67
+ context "when a block is given" do
68
+ it "use the block" do
69
+ user = User.new
70
+ form = Form.new(user: user, category: Category.new)
71
+ allow(form).to receive(:valid?).and_return(true)
72
+ expect(user).to receive(:process)
73
+ form.save { |f| f.user.process }
74
+ end
75
+ end
76
+ end
77
+
78
+ context "when the form is invalid" do
79
+ it "return false" do
80
+ form = Form.new(user: User.new, category: Category.new)
81
+ allow(form).to receive(:valid?).and_return(false)
82
+ expect(form.save).to eq false
83
+ end
84
+ end
85
+ end
86
+
87
+ describe "#save!" do
88
+ context "when no block is given" do
89
+ it "save all models" do
90
+ user = User.new
91
+ category = Category.new
92
+ form = Form.new(user: user, category: category)
93
+ expect(user).to receive(:save!)
94
+ expect(category).to receive(:save!)
95
+ form.save!
96
+ end
97
+
98
+ it "is surrounded by a transaction" do
99
+ expect(ActiveRecord::Base).to receive(:transaction).at_least(:once).
100
+ and_yield
101
+ form = Form.new(user: User.new, category: Category.new)
102
+ form.save!
103
+ end
104
+ end
105
+
106
+ context "when a block is given" do
107
+ it "use the block" do
108
+ user = User.new
109
+ form = Form.new(user: user, category: Category.new)
110
+ expect(user).to receive(:process)
111
+ form.save! { |f| f.user.process }
112
+ end
113
+ end
114
+ end
115
+
116
+ describe "#main_model" do
117
+ it "give the main model" do
118
+ Form.main_model = :category
119
+ category = Category.new
120
+ form = Form.new(user: User.new, category: category)
121
+ expect(form.main_model).to eq category
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveForm::ValidateUniqueness do
4
+ class Form
5
+ include ActiveForm::Form
6
+ include ActiveForm::ValidateUniqueness
7
+ properties :name, on: :user
8
+ validates_uniqueness_of :name, :user
9
+ end
10
+
11
+ context "when there is no user" do
12
+ it "is valid" do
13
+ user = User.new
14
+ form = Form.new(user: user)
15
+ expect(form).to be_valid
16
+ end
17
+ end
18
+
19
+ context "when there is user with the same email" do
20
+ context "when the user is saved" do
21
+ it "is valid" do
22
+ user = User.create(name: 'name')
23
+ form = Form.new(user: user)
24
+ expect(form).to be_valid
25
+ end
26
+ end
27
+
28
+ context "when the user is not saved" do
29
+ it "is not valid" do
30
+ User.create(name: 'name')
31
+ user = User.new(name: 'name')
32
+ form = Form.new(user: user)
33
+ expect(form).to_not be_valid
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rails', '>= 4.0.0'
4
+ gem 'sqlite3'
5
+ gem 'simple_form'
6
+
7
+ gem 'pry', group: 'development'
@@ -0,0 +1,94 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ actionmailer (4.0.2)
5
+ actionpack (= 4.0.2)
6
+ mail (~> 2.5.4)
7
+ actionpack (4.0.2)
8
+ activesupport (= 4.0.2)
9
+ builder (~> 3.1.0)
10
+ erubis (~> 2.7.0)
11
+ rack (~> 1.5.2)
12
+ rack-test (~> 0.6.2)
13
+ activemodel (4.0.2)
14
+ activesupport (= 4.0.2)
15
+ builder (~> 3.1.0)
16
+ activerecord (4.0.2)
17
+ activemodel (= 4.0.2)
18
+ activerecord-deprecated_finders (~> 1.0.2)
19
+ activesupport (= 4.0.2)
20
+ arel (~> 4.0.0)
21
+ activerecord-deprecated_finders (1.0.3)
22
+ activesupport (4.0.2)
23
+ i18n (~> 0.6, >= 0.6.4)
24
+ minitest (~> 4.2)
25
+ multi_json (~> 1.3)
26
+ thread_safe (~> 0.1)
27
+ tzinfo (~> 0.3.37)
28
+ arel (4.0.2)
29
+ atomic (1.1.14)
30
+ builder (3.1.4)
31
+ coderay (1.1.0)
32
+ erubis (2.7.0)
33
+ hike (1.2.3)
34
+ i18n (0.6.9)
35
+ mail (2.5.4)
36
+ mime-types (~> 1.16)
37
+ treetop (~> 1.4.8)
38
+ method_source (0.8.2)
39
+ mime-types (1.25.1)
40
+ minitest (4.7.5)
41
+ multi_json (1.8.4)
42
+ polyglot (0.3.3)
43
+ pry (0.9.12.6)
44
+ coderay (~> 1.0)
45
+ method_source (~> 0.8)
46
+ slop (~> 3.4)
47
+ rack (1.5.2)
48
+ rack-test (0.6.2)
49
+ rack (>= 1.0)
50
+ rails (4.0.2)
51
+ actionmailer (= 4.0.2)
52
+ actionpack (= 4.0.2)
53
+ activerecord (= 4.0.2)
54
+ activesupport (= 4.0.2)
55
+ bundler (>= 1.3.0, < 2.0)
56
+ railties (= 4.0.2)
57
+ sprockets-rails (~> 2.0.0)
58
+ railties (4.0.2)
59
+ actionpack (= 4.0.2)
60
+ activesupport (= 4.0.2)
61
+ rake (>= 0.8.7)
62
+ thor (>= 0.18.1, < 2.0)
63
+ rake (10.1.1)
64
+ simple_form (3.0.1)
65
+ actionpack (>= 4.0.0, < 4.1)
66
+ activemodel (>= 4.0.0, < 4.1)
67
+ slop (3.4.7)
68
+ sprockets (2.10.1)
69
+ hike (~> 1.2)
70
+ multi_json (~> 1.0)
71
+ rack (~> 1.0)
72
+ tilt (~> 1.1, != 1.3.0)
73
+ sprockets-rails (2.0.1)
74
+ actionpack (>= 3.0)
75
+ activesupport (>= 3.0)
76
+ sprockets (~> 2.8)
77
+ sqlite3 (1.3.8)
78
+ thor (0.18.1)
79
+ thread_safe (0.1.3)
80
+ atomic
81
+ tilt (1.4.1)
82
+ treetop (1.4.15)
83
+ polyglot
84
+ polyglot (>= 0.3.1)
85
+ tzinfo (0.3.38)
86
+
87
+ PLATFORMS
88
+ ruby
89
+
90
+ DEPENDENCIES
91
+ pry
92
+ rails (>= 4.0.0)
93
+ simple_form
94
+ sqlite3