minimapper 0.2.4 → 0.2.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/minimapper/ar.rb +10 -2
- data/lib/minimapper/version.rb +1 -1
- data/spec/ar_spec.rb +37 -1
- data/spec/support/database_setup.rb +1 -0
- metadata +6 -6
data/README.md
CHANGED
@@ -274,6 +274,10 @@ You need mysql and postgres installed (but they do not have to be running) to be
|
|
274
274
|
|
275
275
|
### Next
|
276
276
|
|
277
|
+
* Make it possible to override minimapper attributes with super (like it's done in https://github.com/barsoom/traco)
|
278
|
+
* Support default values for attributes (probably only using lambdas to avoid bugs).
|
279
|
+
* Built in way to set induvidual attributes in a way that bypasses protected attributes like you can do with an AR model.
|
280
|
+
- user.is_admin = true; user_mapper.update(user) should probably set is_admin to true, mass-assignment should not.
|
277
281
|
* Extract entity and model class lookup code from the ar-mapper and reuse it in the memory mapper.
|
278
282
|
* Change the memory mapper to store entity attributes, not entity instances.
|
279
283
|
- Unless this makes it difficult to handle associated data.
|
data/lib/minimapper/ar.rb
CHANGED
@@ -7,7 +7,7 @@ module Minimapper
|
|
7
7
|
# Create
|
8
8
|
def create(entity)
|
9
9
|
if entity.valid?
|
10
|
-
entity.id = record_klass.create!(entity
|
10
|
+
entity.id = record_klass.create!(accessible_attributes(entity)).id
|
11
11
|
else
|
12
12
|
false
|
13
13
|
end
|
@@ -41,7 +41,7 @@ module Minimapper
|
|
41
41
|
# Update
|
42
42
|
def update(entity)
|
43
43
|
if entity.valid?
|
44
|
-
record_for(entity).update_attributes!(entity
|
44
|
+
record_for(entity).update_attributes!(accessible_attributes(entity))
|
45
45
|
true
|
46
46
|
else
|
47
47
|
false
|
@@ -81,6 +81,14 @@ module Minimapper
|
|
81
81
|
@entity_class
|
82
82
|
end
|
83
83
|
|
84
|
+
def accessible_attributes(entity)
|
85
|
+
entity.attributes.reject { |k, v| protected_attributes.include?(k.to_s) }
|
86
|
+
end
|
87
|
+
|
88
|
+
def protected_attributes
|
89
|
+
record_klass.protected_attributes
|
90
|
+
end
|
91
|
+
|
84
92
|
def find_record_safely(id)
|
85
93
|
find_record(id) ||
|
86
94
|
raise(Common::CanNotFindEntity, :id => id)
|
data/lib/minimapper/version.rb
CHANGED
data/spec/ar_spec.rb
CHANGED
@@ -18,8 +18,10 @@ class TestMapper < Minimapper::AR
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class Record < ActiveRecord::Base
|
21
|
-
|
21
|
+
attr_protected :visible
|
22
|
+
|
22
23
|
self.table_name = :projects
|
24
|
+
self.mass_assignment_sanitizer = :strict
|
23
25
|
end
|
24
26
|
end
|
25
27
|
|
@@ -28,4 +30,38 @@ describe Minimapper::AR do
|
|
28
30
|
let(:entity_class) { TestEntity }
|
29
31
|
|
30
32
|
include_examples :mapper
|
33
|
+
|
34
|
+
describe "#create" do
|
35
|
+
it "does not include protected attributes" do
|
36
|
+
# because it leads to exceptions when mass_assignment_sanitizer is set to strict
|
37
|
+
entity = TestEntity.new
|
38
|
+
entity.attributes = { :visible => true, :name => "Joe" }
|
39
|
+
mapper.create(entity)
|
40
|
+
|
41
|
+
stored_entity = mapper.find(entity.id)
|
42
|
+
stored_entity.attributes[:visible].should be_nil
|
43
|
+
stored_entity.attributes[:name].should == "Joe"
|
44
|
+
|
45
|
+
entity = TestEntity.new
|
46
|
+
entity.attributes = { :visible => true, :name => "Joe" }
|
47
|
+
TestMapper::Record.stub(:protected_attributes => [])
|
48
|
+
lambda { mapper.create(entity) }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
describe "#update" do
|
53
|
+
it "does not include protected attributes" do
|
54
|
+
entity = TestEntity.new
|
55
|
+
mapper.create(entity)
|
56
|
+
|
57
|
+
entity.attributes = { :visible => true, :name => "Joe" }
|
58
|
+
mapper.update(entity)
|
59
|
+
stored_entity = mapper.find(entity.id)
|
60
|
+
stored_entity.attributes[:visible].should be_nil
|
61
|
+
stored_entity.attributes[:name].should == "Joe"
|
62
|
+
|
63
|
+
TestMapper::Record.stub(:protected_attributes => [])
|
64
|
+
lambda { mapper.update(entity) }.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
|
65
|
+
end
|
66
|
+
end
|
31
67
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: minimapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-12-
|
12
|
+
date: 2012-12-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
16
|
-
requirement: &
|
16
|
+
requirement: &70274703776420 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70274703776420
|
25
25
|
description: A minimalistic way of separating your models from ORMs like ActiveRecord.
|
26
26
|
email:
|
27
27
|
- joakim.kolsjo@gmail.com
|
@@ -77,7 +77,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
77
77
|
version: '0'
|
78
78
|
segments:
|
79
79
|
- 0
|
80
|
-
hash: -
|
80
|
+
hash: -1932883242687325558
|
81
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
82
|
none: false
|
83
83
|
requirements:
|
@@ -86,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
86
86
|
version: '0'
|
87
87
|
segments:
|
88
88
|
- 0
|
89
|
-
hash: -
|
89
|
+
hash: -1932883242687325558
|
90
90
|
requirements: []
|
91
91
|
rubyforge_project:
|
92
92
|
rubygems_version: 1.8.5
|