fob 0.0.4 → 0.0.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3cd2dcda585fe81a6220c55a7b6041b6fc05e563
4
- data.tar.gz: a597563ff3fd137eb875689a2d60e7c11f9a7eca
3
+ metadata.gz: 7094a352ce945f8266825de641ada2d9b3884dcf
4
+ data.tar.gz: 5cca845c4191e64779a775f03226a8bae09beb30
5
5
  SHA512:
6
- metadata.gz: 34bbe07f9e8cbdf869104a79ad7f6d30c174e1d1272b1e976c6272a8b570d9e37756d41a5bddc00e37516c496769128074f4e17156d89460274476401b17a147
7
- data.tar.gz: 112972f52afaa1d14d213f8c30f12ed4aa66a50eb081f100417df93034c2ce5ca1813ed0bbbb42c962baf0927830d512f6fbf49ac86b1c8d703900826d3b72a5
6
+ metadata.gz: 85391b4678131b5092fbdfce458d3aa382d6858a5ca74b92cbf8d4ca01aa58aa961ee65ecc71b9c07d7d6c695070d50d63dbe0ff231054167cf49f1f0e7c69c9
7
+ data.tar.gz: 78eb62e913a45659524bc2dddd77f747e4f2a51690de49c40b290d8da0c012debdba630e77feddab4b7f161ee20eb7965b1e3c8ab249a45859afc1ad919a8248
data/README.md CHANGED
@@ -18,13 +18,41 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- class MyForm < Fob::Fob
22
- represents :user, with: :username, :email
23
- has_one :company, with: :name
24
- attribute :remember_me, Boolean
25
- validates :username, presence: true
26
- validates :email, presence: true
27
- end
21
+ ```ruby
22
+ class MyForm < Fob::Fob
23
+ represents :user, with: :username, :email
24
+ has_one :company, with: :name
25
+ attribute :remember_me, Boolean
26
+ validates :username, presence: true
27
+ validates :email, presence: true
28
+ end
29
+ ```
30
+
31
+ It supports saving and persistance checking based on your definition of saving.
32
+ The `save` method will yield the attributes hash to your provided block.
33
+ Your block should exit with true/false.
34
+
35
+ ```ruby
36
+ MyForm.save |attributes|
37
+ # Do some logic to save
38
+ true
39
+ end
40
+ ```
41
+
42
+ It supports checking uniqueness using the `uniqueness_on` validator.
43
+ To use the validator you must secify which class should be used to test for uniqueness.
44
+ The `exists?` method will be called on the class and is given a hash of the attribute name and value.
45
+ This should work as-is with an ActiveRecord model and is straightforward to implement on other class types.
46
+
47
+ ```ruby
48
+ class MyForm < Fob::Fob
49
+ represents :user, with: :username, :email
50
+ has_one :company, with: :name
51
+ attribute :remember_me, Boolean
52
+ validates :username, presence: true
53
+ validates :email, presence: true, uniqueness_on: { class_name: 'User' }
54
+ end
55
+ ```
28
56
 
29
57
  ## Contributing
30
58
 
data/Rakefile CHANGED
@@ -3,4 +3,12 @@ require "rspec/core/rake_task"
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task :default => :spec
7
+
8
+ task :console do
9
+ require 'irb'
10
+ require 'irb/completion'
11
+ require 'fob'
12
+ ARGV.clear
13
+ IRB.start
14
+ end
@@ -0,0 +1,16 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class UniquenessOnValidator < EachValidator
4
+ def validate_each(record, attr_name, value)
5
+ klass = options[:class_name].to_s.camelcase.constantize
6
+ if klass.exists?(attr_name.to_sym => value)
7
+ record.errors.add(attr_name, :taken, filtered_options)
8
+ end
9
+ end
10
+
11
+ def filtered_options
12
+ options.except(:class_name)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,3 +1,5 @@
1
+ require 'active_model/validations/uniqueness_on_validator'
2
+
1
3
  module Fob
2
4
 
3
5
  class Fob
@@ -8,7 +10,7 @@ module Fob
8
10
 
9
11
  def self.represents(name, options={})
10
12
  include_model(name, options)
11
- klass = name.to_s.split("_").collect(&:capitalize).join
13
+ klass = name.to_s.camelcase
12
14
  singleton_class.instance_eval do
13
15
  define_method(:model_name) do
14
16
  ::ActiveModel::Name.new(self, nil, "#{klass}")
@@ -20,8 +22,17 @@ module Fob
20
22
  include_model(name, options)
21
23
  end
22
24
 
25
+ def initialize(*args, &block)
26
+ super
27
+ @persisted = false
28
+ end
29
+
30
+ def save
31
+ @persisted = yield attributes
32
+ end
33
+
23
34
  def persisted?
24
- false
35
+ @persisted
25
36
  end
26
37
 
27
38
  private
@@ -1,3 +1,3 @@
1
1
  module Fob
2
- VERSION = "0.0.4"
2
+ VERSION = '0.0.5'
3
3
  end
@@ -38,4 +38,64 @@ describe Fob::Fob do
38
38
  specify { expect(form.new.persisted?).to eq false }
39
39
  end
40
40
 
41
- end
41
+ describe '#save' do
42
+ let(:form) do
43
+ Class.new(Fob::Fob) do
44
+ attribute :property, String
45
+ end
46
+ end
47
+
48
+ specify { expect { |b| form.new(property: 'foo').save(&b) }.to yield_with_args(property: 'foo') }
49
+
50
+ it 'updates persisted?' do
51
+ f = form.new(property: 'foo')
52
+ expect(f).not_to be_persisted
53
+ f.save { true }
54
+ expect(f).to be_persisted
55
+ end
56
+ end
57
+
58
+ class FailingModel
59
+ def self.exists?(h)
60
+ true
61
+ end
62
+ end
63
+
64
+ class PassingModel
65
+ def self.exists?(h)
66
+ false
67
+ end
68
+ end
69
+
70
+ describe 'uniqueness' do
71
+
72
+ context 'with a failing validation' do
73
+ let(:form) do
74
+ Class.new(Fob::Fob) do
75
+ def self.model_name
76
+ ::ActiveModel::Name.new(self, nil, 'test')
77
+ end
78
+ attribute :property, String
79
+ validates :property, uniqueness_on: { class_name: :failing_model }
80
+ end
81
+ end
82
+
83
+ specify { expect(form.new).not_to be_valid }
84
+ end
85
+
86
+ context 'with a failing validation' do
87
+ let(:form) do
88
+ Class.new(Fob::Fob) do
89
+ def self.model_name
90
+ ::ActiveModel::Name.new(self, nil, 'test')
91
+ end
92
+ attribute :property, String
93
+ validates :property, uniqueness_on: { class_name: :passing_model }
94
+ end
95
+ end
96
+
97
+ specify { expect(form.new).to be_valid }
98
+ end
99
+ end
100
+
101
+ end
metadata CHANGED
@@ -1,97 +1,97 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Peachey
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-02-27 00:00:00.000000000 Z
11
+ date: 2014-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: virtus
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: 1.0.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 1.0.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activemodel
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '3.2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.2'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bundler
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ~>
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
47
  version: '1.3'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ~>
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.3'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 10.0.4
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: 10.0.4
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: 2.14.1
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ~>
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: 2.14.1
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: simplecov
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ~>
87
+ - - "~>"
88
88
  - !ruby/object:Gem::Version
89
89
  version: 0.7.1
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ~>
94
+ - - "~>"
95
95
  - !ruby/object:Gem::Version
96
96
  version: 0.7.1
97
97
  description: Form OBjects made easy.
@@ -101,12 +101,13 @@ executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - .gitignore
104
+ - ".gitignore"
105
105
  - Gemfile
106
106
  - LICENSE.txt
107
107
  - README.md
108
108
  - Rakefile
109
109
  - fob.gemspec
110
+ - lib/active_model/validations/uniqueness_on_validator.rb
110
111
  - lib/fob.rb
111
112
  - lib/fob/fob.rb
112
113
  - lib/fob/version.rb
@@ -122,17 +123,17 @@ require_paths:
122
123
  - lib
123
124
  required_ruby_version: !ruby/object:Gem::Requirement
124
125
  requirements:
125
- - - '>='
126
+ - - ">="
126
127
  - !ruby/object:Gem::Version
127
128
  version: '0'
128
129
  required_rubygems_version: !ruby/object:Gem::Requirement
129
130
  requirements:
130
- - - '>='
131
+ - - ">="
131
132
  - !ruby/object:Gem::Version
132
133
  version: '0'
133
134
  requirements: []
134
135
  rubyforge_project:
135
- rubygems_version: 2.2.1
136
+ rubygems_version: 2.2.2
136
137
  signing_key:
137
138
  specification_version: 4
138
139
  summary: Don't bulk up your models or controllers. Stay away from accepts_nested_attributes_for.