mor 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mor/model.rb +16 -7
- data/lib/mor/version.rb +1 -1
- data/spec/model_spec.rb +31 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1afbeb537134b7328ed4769fda161403f81adf4
|
4
|
+
data.tar.gz: 622c07371fd2d4682019fa3deba77d48fddb844c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fdef1f7bcb95126581c473fe6e98ffa04d9b1888498efecb6d7b3922d31ebed1e28a2b15586b39e4db26f2ff48c0ccb79d66cff53fdead486669f14fe9a5aada
|
7
|
+
data.tar.gz: 2d3e945cc5529cddcc99cc44d3c094c03b909e52c628050d7b26dac2791dec82cba7f0eaf892b161d18757ba76b46d3ed4ed771632b9888f0b14a33fb5239c87
|
data/lib/mor/model.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'active_model'
|
2
2
|
require 'active_support/concern'
|
3
3
|
require 'active_support/hash_with_indifferent_access'
|
4
|
+
require 'active_support/core_ext/hash'
|
4
5
|
|
5
6
|
module Mor
|
6
7
|
module Model
|
@@ -10,7 +11,10 @@ module Mor
|
|
10
11
|
def attr_accessor *attrs
|
11
12
|
super(*attrs)
|
12
13
|
attrs.delete_if{ |a| a == :attributes }.each { |attr|
|
13
|
-
define_method(:"#{attr}="){|val|
|
14
|
+
define_method(:"#{attr}="){ |val|
|
15
|
+
self.attributes[attr]=val
|
16
|
+
self.attributes[:id]=val if attr == self.class.primary_key && attr != :id
|
17
|
+
}
|
14
18
|
define_method(attr){ self.attributes[attr] }
|
15
19
|
}
|
16
20
|
end
|
@@ -43,11 +47,16 @@ module Mor
|
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
46
|
-
def
|
50
|
+
def where hash
|
47
51
|
return nil unless hash
|
48
52
|
self.all.select{ |app|
|
49
53
|
app.attributes.select{ |k,v| hash.keys.include?(k.to_sym) }.values.sort == hash.values.sort
|
50
|
-
}
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def find_by hash
|
58
|
+
return nil unless hash
|
59
|
+
where(hash).first
|
51
60
|
end
|
52
61
|
|
53
62
|
def all
|
@@ -75,8 +84,8 @@ module Mor
|
|
75
84
|
|
76
85
|
define_model_callbacks :save, :create, :update, :destroy
|
77
86
|
|
78
|
-
validates_presence_of
|
79
|
-
validate :
|
87
|
+
validates_presence_of self.primary_key
|
88
|
+
validate :validate_uniqueness_of_primary_key, unless: "persisted?"
|
80
89
|
after_create :add_to_index
|
81
90
|
after_destroy :remove_from_index
|
82
91
|
|
@@ -127,8 +136,8 @@ module Mor
|
|
127
136
|
|
128
137
|
private
|
129
138
|
|
130
|
-
def
|
131
|
-
errors.add(
|
139
|
+
def validate_uniqueness_of_primary_key
|
140
|
+
errors.add(self.class.primary_key, :taken) if self.class.index.include?(self.id)
|
132
141
|
end
|
133
142
|
|
134
143
|
def save_or_update
|
data/lib/mor/version.rb
CHANGED
data/spec/model_spec.rb
CHANGED
@@ -12,6 +12,19 @@ class TestModel
|
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
+
class TestModelDiffPK
|
16
|
+
include Mor::Model
|
17
|
+
def self.primary_key
|
18
|
+
:slug
|
19
|
+
end
|
20
|
+
attr_accessor :slug, :title, :body, :slug
|
21
|
+
before_validation :set_slug
|
22
|
+
private
|
23
|
+
def set_slug
|
24
|
+
self.slug = title.downcase
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
15
28
|
describe Mor::Model do
|
16
29
|
|
17
30
|
it "acts as active_model/model" do
|
@@ -52,4 +65,22 @@ describe Mor::Model do
|
|
52
65
|
TestModel.find("test-id").must_be_nil
|
53
66
|
end
|
54
67
|
|
68
|
+
it "can has different primary key than id" do
|
69
|
+
instance = TestModelDiffPK.create(title: "Tett Name")
|
70
|
+
instance.persisted?.must_equal true
|
71
|
+
instance.id.must_equal instance.slug
|
72
|
+
instance.attributes[:id].must_equal instance.attributes[:slug]
|
73
|
+
end
|
74
|
+
|
75
|
+
it "finds single record by attributes" do
|
76
|
+
instance = TestModel.create(id: "test-id", title: "title", body: "test body")
|
77
|
+
TestModel.find_by(title: "title").attributes.must_equal instance.attributes
|
78
|
+
end
|
79
|
+
|
80
|
+
it "finds many records by attributes" do
|
81
|
+
instance = TestModel.create(id: "test-id", title: "title", body: "test body")
|
82
|
+
instance2 = TestModel.create(id: "test-id-2", title: "title", body: "test body")
|
83
|
+
TestModel.where(title: "title").size.must_equal 2
|
84
|
+
end
|
85
|
+
|
55
86
|
end
|