passive_model 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +70 -11
- data/lib/passive_model/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cb48ebe8a0f354a6ef8ecebbbed4f8257854e09844ad57c5c82fdb09ef42e5ad
|
4
|
+
data.tar.gz: ea06363924c66cba7055becfc87b21a2fd025bc65f47b35d944797cb52d237ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c04ee7e7796735256f9dc3182121c272948a76cd00cee3db28d4812ae4cceaf848ea49f2890bddf16a0dabf47a3c1537287386919997a8ebb2a4b8231ea968b2
|
7
|
+
data.tar.gz: f06ec4476ae3e822fed9b494fb96a7c4d2d8a7205ecf44e5eeb5bfb99d88af926b67a4ff162d436d8862a38604ad3ac63f69301f661b0a5eb54d2f54ef93ca81
|
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
|
-
#
|
1
|
+
# PassiveModel
|
2
2
|
|
3
|
-
|
3
|
+
This is a gem for using Rails ActiveModel functionalities without having an actual database table.
|
4
4
|
|
5
|
-
|
5
|
+
Supports validation, callbacks etc. And behaves like a model object, so can be replaced with actual model objects.
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
9
|
Add this line to your application's Gemfile:
|
10
10
|
|
11
11
|
```ruby
|
12
|
-
gem '
|
12
|
+
gem 'passive_model'
|
13
13
|
```
|
14
14
|
|
15
15
|
And then execute:
|
@@ -18,19 +18,78 @@ And then execute:
|
|
18
18
|
|
19
19
|
Or install it yourself as:
|
20
20
|
|
21
|
-
$ gem install
|
21
|
+
$ gem install passive_model
|
22
22
|
|
23
23
|
## Usage
|
24
24
|
|
25
|
-
|
25
|
+
Create a class from `PassiveModel::Base`.
|
26
26
|
|
27
|
-
|
27
|
+
For example:
|
28
28
|
|
29
|
-
|
29
|
+
```erbruby
|
30
|
+
class ContactForm < PassiveModel::Base
|
31
|
+
attr_accessor :first_name, :last_name
|
30
32
|
|
31
|
-
|
33
|
+
validates :first_name, presence: true
|
34
|
+
validates :last_name, presence: true
|
35
|
+
validate :first_name_is_not_spam
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def first_name_is_not_spam
|
40
|
+
return if first_name.downcase != 'test'
|
32
41
|
|
33
|
-
|
42
|
+
errors.add(:first_name, 'is not valid')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
```
|
46
|
+
|
47
|
+
#### Create new objects
|
48
|
+
|
49
|
+
`form = ContactForm.new(first_name: 'John', last_name: 'Doe')`
|
50
|
+
|
51
|
+
#### Set attributes
|
52
|
+
|
53
|
+
`form.attributes = { first_name: 'Lewis' }`
|
54
|
+
|
55
|
+
#### Emulate a save
|
56
|
+
|
57
|
+
It runs the validations and also calls `before_save` callbacks if the object is valid
|
58
|
+
|
59
|
+
`form.save # returns true if valid?`
|
60
|
+
|
61
|
+
#### Emulate a save!
|
62
|
+
|
63
|
+
Raise an error if cannot be saved
|
34
64
|
|
35
|
-
|
65
|
+
`form.save!`
|
66
|
+
|
67
|
+
#### persisted?
|
68
|
+
|
69
|
+
Return if save has been called
|
70
|
+
|
71
|
+
`form.persisted?`
|
72
|
+
|
73
|
+
#### before_save callback
|
74
|
+
|
75
|
+
Add a callback as follows:
|
76
|
+
|
77
|
+
```erbruby
|
78
|
+
class ContactForm < PassiveModel::Base
|
79
|
+
before_save :send_info_to_mailchimp
|
80
|
+
|
81
|
+
validate :first_name, presence: true
|
82
|
+
|
83
|
+
private
|
84
|
+
|
85
|
+
def send_info_to_mailchimp
|
86
|
+
# API call
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
The callback will be executed only if validations are clear.
|
92
|
+
|
93
|
+
## Contributing
|
36
94
|
|
95
|
+
If you need more functionality, for bug reports raise a request at https://github.com/RocketApex/passive_model
|