basic_model 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +19 -2
  2. data/lib/basic_model.rb +24 -4
  3. metadata +19 -5
data/README.md CHANGED
@@ -21,9 +21,26 @@ class Message
21
21
  validates_presence_of :name
22
22
  end
23
23
 
24
- message = Message.new(:email => 'foo', :content => 'bar')
24
+ message = Message.new(:email => 'bob@example.com', :content => 'Hey man!')
25
25
  message.valid?
26
- message.errors[:name] # => ['can't be blank']
26
+ message.errors[:name] # => ["can't be blank"]
27
+ ```
28
+
29
+ Using `ActiveModel::MassAssignmentSecurity`:
30
+
31
+ ```ruby
32
+ require 'active_model'
33
+ require 'basic_model'
34
+
35
+ class Comment
36
+ include BasicModel
37
+ include ActiveModel::MassAssignmentSecurity
38
+ attr_accessor :name, :comment, :spam
39
+ attr_accessible :name, :comment
40
+ end
41
+
42
+ comment = Comment.new(:name => 'Bob', :comment => 'Great Post!', :spam => false)
43
+ comment.spam # => nil
27
44
  ```
28
45
 
29
46
  ## Copyright
data/lib/basic_model.rb CHANGED
@@ -6,12 +6,32 @@ module BasicModel
6
6
  include ActiveModel::Conversion
7
7
  include ActiveModel::Validations
8
8
 
9
- def initialize(attributes = {})
10
- attributes.each do |name, value|
11
- send "#{name}=", value
12
- end unless attributes.nil?
9
+ # Creates and optionally sets the attributes of the model with keys
10
+ # matching the attribute names. If ActiveModel::MassSecurityAssignment
11
+ # is included, attributes will be sanitized with the role given in the :as option.
12
+ # To bypass mass-assignment security you can use the :without_protection => true option.
13
+ def initialize(attributes = nil, options = {})
14
+ unless attributes.nil?
15
+ # Sanitize the attributes if we're using ActiveModel::MassSecurityAssignment
16
+ # Also account for the different APIs between 3.0 and 3.1
17
+ if respond_to? :sanitize_for_mass_assignment
18
+ if method(:sanitize_for_mass_assignment).arity == 2
19
+ attributes = sanitize_for_mass_assignment attributes, options[:as]
20
+ else
21
+ attributes = sanitize_for_mass_assignment attributes
22
+ end
23
+ end
24
+
25
+ # Now set the attributes, ignoring any private methods
26
+ attributes.each do |name, value|
27
+ send "#{name}=", value if respond_to? "#{name}="
28
+ end
29
+ end
30
+
31
+ yield self if block_given?
13
32
  end
14
33
 
34
+ # By default, BasicModels are not persisted in any way.
15
35
  def persisted?
16
36
  false
17
37
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: basic_model
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Pete Browne
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-19 00:00:00 Z
18
+ date: 2011-08-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: activemodel
@@ -47,6 +47,20 @@ dependencies:
47
47
  version: "3.0"
48
48
  type: :runtime
49
49
  version_requirements: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ name: rake
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
62
+ type: :development
63
+ version_requirements: *id003
50
64
  description: BasicModel is intended to be used in a tableless model in Rails >= 3.0. It complies with the ActiveModel API and includes valdiations, so it can be used in form helpers without a problem.
51
65
  email: me@petebrowne.com
52
66
  executables: []