simple_model 1.0.0 → 1.0.1

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple_model (0.2.6)
4
+ simple_model (1.0.0)
5
5
  activemodel (~> 3.0)
6
6
  activesupport (~> 3.0)
7
7
 
@@ -105,16 +105,22 @@ module SimpleModel
105
105
  def self.update(*methods)
106
106
  define_model_action(methods,:update)
107
107
  end
108
-
109
- def self.destroy(*methods)
110
- define_model_action(methods,:destroy)
108
+
109
+ #Destroy does not run normal validation by default.
110
+ def self.destroy(*methods)
111
+ define_model_action(methods,:destroy, {:validate => false})
111
112
  end
112
113
 
113
114
  private
114
115
 
116
+
117
+ # Skeleton for action instance methods
115
118
  def run_model_action(methods,options)
116
119
  completed = true
117
- if self.valid?
120
+ if !options[:validate] ||
121
+ (options[:validation_methods] && valid_using_other?(options[:validation_methods])) ||
122
+ self.valid?
123
+
118
124
  methods.each do |method|
119
125
  ran = self.send(method)
120
126
  completed = ran unless ran
@@ -130,9 +136,26 @@ module SimpleModel
130
136
  completed
131
137
  end
132
138
 
133
- def self.define_model_action(methods,action)
134
- options = methods.extract_options!
135
- define_method(action) do
139
+
140
+ # Run supplied methods as valdation. Each method should return a boolean
141
+ # If using this option, to see if errors are present use object_name.errors.blank?,
142
+ # otherwise if you run object_name.valid? you will over write the errors
143
+ # generated here.
144
+ def valid_using_other?(methods)
145
+ valid = true
146
+ methods.each do |method|
147
+ valid = false unless self.send(method)
148
+ end
149
+ valid
150
+ end
151
+
152
+
153
+ # Defines the model action's instantace methods and applied defaults.
154
+ # Defines methods with :validate options as true by default.
155
+ def self.define_model_action(methods,action,default_options={:validate => true})
156
+ default_options.merge!(methods.extract_options!)
157
+ define_method(action) do |opts={}|
158
+ options = default_options.merge(opts)
136
159
  self.run_callbacks(action) do
137
160
  run_model_action(methods,options)
138
161
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleModel
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,24 +1,59 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
3
  describe SimpleModel do
4
-
5
- describe "save" do
4
+ context 'action methods' do
5
+ describe "save" do
6
6
 
7
- it "should perform the supplied methods" do
8
- class TestStuff < SimpleModel::Base
9
- save :test
7
+ it "should perform the supplied methods" do
8
+ class TestStuff < SimpleModel::Base
9
+ save :test
10
10
 
11
- attr_accessor :foo
11
+ attr_accessor :foo
12
12
 
13
- def test
14
- self.foo = "test"
15
- return true
13
+ def test
14
+ self.foo = "test"
15
+ return true
16
+ end
16
17
  end
18
+
19
+ t = TestStuff.new
20
+ t.save
21
+ t.foo.should eql("test")
22
+ end
23
+
24
+ it "should be false if validation fails" do
25
+ class TestStuff < SimpleModel::Base
26
+ save :test
27
+ has_decimal :price
28
+ validates_inclusion_of :price, :in => 10..25
29
+ validates :price, :presence => true
30
+
31
+
32
+ def test
33
+ true
34
+ end
35
+ end
36
+
37
+ t = TestStuff.new
38
+ t.save.should be_false
17
39
  end
40
+ end
41
+
42
+ describe "destroy" do
43
+
44
+ it "should not preform validation by default" do
45
+ class TestStuff < SimpleModel::Base
46
+ destroy :test
47
+ attr_accessor :foo
48
+ validate :foo, :presence => true
49
+ def test
50
+ return true
51
+ end
52
+ end
18
53
 
19
- t = TestStuff.new
20
- t.save
21
- t.foo.should eql("test")
54
+ t = TestStuff.new
55
+ t.destroy.should be_true
56
+ end
22
57
  end
23
58
  end
24
59
 
@@ -83,12 +118,9 @@ describe SimpleModel do
83
118
  t.bar.should eql('bar')
84
119
  end
85
120
 
86
- it "should run call backs on save" do
121
+ it "should run save and validation callbacks on save" do
87
122
  class TestStuff < SimpleModel::Base
88
- save do
89
- puts "saved"
90
- true
91
- end
123
+ save :my_save_method
92
124
  before_save :set_foo
93
125
  after_validation :set_bar
94
126
  attr_accessor :foo,:bar
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-10-24 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70349578811140 !ruby/object:Gem::Requirement
16
+ requirement: &70355814091020 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70349578811140
24
+ version_requirements: *70355814091020
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activemodel
27
- requirement: &70349578810640 !ruby/object:Gem::Requirement
27
+ requirement: &70355814090520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '3.0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70349578810640
35
+ version_requirements: *70355814090520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rspec
38
- requirement: &70349578810260 !ruby/object:Gem::Requirement
38
+ requirement: &70355814090140 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70349578810260
46
+ version_requirements: *70355814090140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: autotest
49
- requirement: &70349578809800 !ruby/object:Gem::Requirement
49
+ requirement: &70355814089680 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,7 +54,7 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70349578809800
57
+ version_requirements: *70355814089680
58
58
  description: Simpifies building tableless models or models backed by webservices.
59
59
  Create data type specific attributes with default if values.
60
60
  email: