aform 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe5b2d60a9140c2a2477d80b1c6eb8a9a1d7efe7
4
- data.tar.gz: 64377b994360f01535d6d94e9933dd026cb6bda9
3
+ metadata.gz: 93ff36848be09d703e47acb5b73764f97f235ce5
4
+ data.tar.gz: bcbac1680a8d4ced2f1bfcaa74170b4ab0ec0052
5
5
  SHA512:
6
- metadata.gz: 7aca61c011ecbfa66e5b810fa514e3a09ef786b57f0a93fbe97ee156ac86bc25832997f37d556693f11b7c2bddaa4bb8e8e98960dac5128d34c838dc70d7c196
7
- data.tar.gz: 4dd679c64d3cda49eab1ca903d8071bf3eaf06fab3b9923c399ad62c69c3751807e62bbcd4d72b9f30f2aa2033e906ff1346b625ca00db86e668dc01bc23e6bc
6
+ metadata.gz: f55367787834ae50febed73521daf8a8642a413d91224ed1bdba7924f189fe072d31021603495fa03b99d2e0c6be6abfa5f90442ba8d529edc3cd66d5c630896
7
+ data.tar.gz: adf3e582fbc4a2fee4d7895af355a6180d044f00bd083dfac303e4c8cc636a986204cbf438061d26a21967483266444e1d66f409fae42d059cfaaf5c65c684f7
data/lib/aform/errors.rb CHANGED
@@ -14,7 +14,8 @@ module Aform
14
14
  if nf = form.nested_forms
15
15
  nf.inject({}) do |memo, (k,v)|
16
16
  messages = v.each_with_index.inject({}) do |m, (e, i)|
17
- m.merge(i => e.form_model.errors.messages.merge(nested_messages(e)))
17
+ errors = e.form_model.errors.messages.merge(nested_messages(e))
18
+ errors.present? ? m.merge(i => errors) : m
18
19
  end
19
20
  memo.merge(k => messages)
20
21
  end
data/lib/aform/form.rb CHANGED
@@ -1,8 +1,6 @@
1
1
  module Aform
2
2
  class Form
3
- class_attribute :params
4
- class_attribute :validations
5
- class_attribute :nested_form_klasses
3
+ class_attribute :params, :pkey, :validations, :nested_form_klasses
6
4
 
7
5
  attr_reader :form_model, :attributes, :nested_forms, :model, :nested_models, :parent
8
6
 
@@ -41,6 +39,10 @@ module Aform
41
39
  end
42
40
 
43
41
  class << self
42
+ def primary_key(key)
43
+ self.pkey = key
44
+ end
45
+
44
46
  def param(*args)
45
47
  self.params ||= []
46
48
  options = args.extract_options!
@@ -85,7 +87,7 @@ module Aform
85
87
  attributes[k].each do |attrs|
86
88
  @nested_forms ||= {}
87
89
  @nested_forms[k] ||= []
88
- model = nested_ar_model(k, attrs)
90
+ model = nested_ar_model(k, attrs, v.pkey)
89
91
  @nested_forms[k] << v.new(model, attrs, self, @model_klass, @model_builder,
90
92
  @errors_klass, @form_saver, @transaction_klass)
91
93
  end
@@ -94,13 +96,10 @@ module Aform
94
96
  end
95
97
  end
96
98
 
97
- def nested_ar_model(association, attrs)
99
+ def nested_ar_model(association, attrs, key = :id)
100
+ key = key || :id
98
101
  klass = association.to_s.classify.constantize
99
- if attrs.has_key? :id
100
- klass.find(attrs[:id])
101
- else
102
- klass.new
103
- end
102
+ klass.find_by(key => attrs[key]) || klass.new
104
103
  end
105
104
  end
106
105
  end
data/lib/aform/model.rb CHANGED
@@ -14,13 +14,17 @@ class Aform::Model
14
14
  ActiveModel::Name.new(self, nil, "Aform::Model")
15
15
  end
16
16
 
17
+ def self.validates_uniqueness_of(*attr_names)
18
+ validates_with UniquenessValidator, _merge_attributes(attr_names)
19
+ end
20
+
17
21
  # AR saves children with parent if it's new object
18
22
  # but dont save children with parent when children is updated
19
23
  def save(association = nil)
24
+ @object.assign_attributes(@attributes)
20
25
  if @destroy
21
26
  @object.destroy
22
27
  else
23
- @object.assign_attributes(@attributes)
24
28
  association << @object if association
25
29
  @object.save
26
30
  end
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+ class UniquenessValidator < ::ActiveRecord::Validations::UniquenessValidator
3
+ def validate_each(form, attribute, value)
4
+ object = form.instance_variable_get(:@object)
5
+ @klass = object.class
6
+ super(object, attribute, value).tap do |res|
7
+ form.errors.add(attribute, object.errors.first.last) if object.errors.present?
8
+ end
9
+ end
10
+ end
data/lib/aform/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Aform
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/aform.rb CHANGED
@@ -9,6 +9,7 @@ require 'aform/model'
9
9
  require 'aform/builder'
10
10
  require 'aform/errors'
11
11
  require 'aform/form_saver'
12
+ require 'aform/uniqueness_validator'
12
13
 
13
14
  module Aform
14
15
  # Your code goes here...
@@ -150,4 +150,35 @@ describe "saving" do
150
150
  post.author.must_equal("John Doe and Mr. Author")
151
151
  end
152
152
  end
153
+
154
+ context "validate uniqueness" do
155
+ class Other2PostForm < Aform::Form
156
+ param :title
157
+ validates :title, uniqueness: true
158
+
159
+ has_many :comments do
160
+ param :message, :author
161
+ validates_uniqueness_of :message
162
+ end
163
+ end
164
+
165
+ before do
166
+ Post.create!(title: "test")
167
+ Comment.create!(message: "test")
168
+ end
169
+
170
+ after do
171
+ Post.delete_all
172
+ Comment.delete_all
173
+ end
174
+
175
+ it "inherits attribute from parent" do
176
+ post = Post.new
177
+ attrs = {title: "test", comments: [{message: "test"}]}
178
+ form = Other2PostForm.new(post, attrs)
179
+ form.save.must_equal false
180
+ form.errors.must_equal({:title=>["has already been taken"],
181
+ :comments=>{0=>{:message=>["has already been taken"]}}})
182
+ end
183
+ end
153
184
  end
data/test/model_test.rb CHANGED
@@ -111,11 +111,16 @@ describe Aform::Model do
111
111
  end
112
112
 
113
113
  context "when marked for destruction" do
114
- let(:form_model) { subject.new(ar_model, _destroy: true)}
114
+ let(:form_model) { subject.new(ar_model, name: "name", count: 2, _destroy: true)}
115
115
  it "removes element" do
116
116
  ar_model.expects(:destroy).returns(true)
117
117
  form_model.nested_save
118
118
  end
119
+
120
+ it "calls `ar_model.assign_attributes`" do
121
+ ar_model.expects(:assign_attributes).with(name: "name", count: 2).returns(true)
122
+ form_model.nested_save
123
+ end
119
124
  end
120
125
  end
121
126
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aform
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Versal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-15 00:00:00.000000000 Z
11
+ date: 2014-07-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -183,6 +183,7 @@ files:
183
183
  - lib/aform/form.rb
184
184
  - lib/aform/form_saver.rb
185
185
  - lib/aform/model.rb
186
+ - lib/aform/uniqueness_validator.rb
186
187
  - lib/aform/version.rb
187
188
  - test/errors_test.rb
188
189
  - test/form_saver_test.rb