static-record 1.0.0.pre.4 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +2 -6
- data/lib/static_record/concerns/query_building_concern.rb +0 -2
- data/lib/static_record/migrations/has_static_record.rb +7 -6
- data/lib/static_record/models/predicates.rb +15 -4
- data/lib/static_record/version.rb +1 -1
- data/spec/models/relation_spec.rb +10 -0
- data/spec/test_app/app/models/roles/role_one.rb +1 -1
- data/spec/test_app/app/models/roles/role_two.rb +4 -0
- data/spec/test_app/spec/has_static_record_spec.rb +6 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b162063f35ae54889eb3667e905da0e9ded7006
|
4
|
+
data.tar.gz: 47cb368f415b140fd8a1edfb0d0871ee557028d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2818d1563d8abb371273626a852150de43df729fd2aa8db28229b56f060115ed43e98a381e7df716c8a832c25fb23357807ea69fabf47f2741c20615d485c000
|
7
|
+
data.tar.gz: df9d29e83a7d6ba39386710e17857790355667c85c04be06e9b0be4bea8dd8084d13fcd1ab6e4115de1e9e55c9c36ca3c460849af01fcf657c3a1282b245b20d
|
data/README.rdoc
CHANGED
@@ -146,12 +146,8 @@ If you have any question or doubt regarding StaticRecord which you cannot find t
|
|
146
146
|
|
147
147
|
If you find a bug please add an issue on GitHub or fork the project and send a pull request.
|
148
148
|
|
149
|
-
==
|
149
|
+
== Future
|
150
150
|
|
151
|
-
As StaticRecord is in active development and a full list of feature is already scheduled, I won't be accepting any feature-oriented pull requests before the 1.0.0 release (hopefully before mi-January).
|
152
|
-
|
153
|
-
Here is what will be available soon:
|
154
151
|
- Better documentation
|
155
|
-
- Support for Date, Datetime and Foreign keys
|
156
|
-
- Joins
|
157
152
|
- Generators
|
153
|
+
- Allow StaticRecord to reference other static records and add possibility to query over relations (joins)
|
@@ -18,19 +18,20 @@ module StaticRecord
|
|
18
18
|
|
19
19
|
def define_setter(table_name, options)
|
20
20
|
define_method("#{table_name}=") do |static_record|
|
21
|
-
unless static_record.class.pkey
|
22
|
-
err = "No primary key has been defined for #{static_record.class}"
|
23
|
-
raise NoPrimaryKey, err
|
24
|
-
end
|
25
|
-
|
26
21
|
table = __method__.to_s.delete('=')
|
27
22
|
options[:class_name] ||= table.camelize
|
28
23
|
superklass = static_record.class.superclass
|
24
|
+
|
29
25
|
unless superklass.to_s == options[:class_name]
|
30
|
-
err = "Record must be an instance of #{options[:class_name]}"
|
26
|
+
err = "Record must be an instance of #{options[:class_name]}, got #{superklass}"
|
31
27
|
raise ClassError, err
|
32
28
|
end
|
33
29
|
|
30
|
+
unless superklass.pkey
|
31
|
+
err = "No primary key has been defined for #{superklass.class}"
|
32
|
+
raise NoPrimaryKey, err
|
33
|
+
end
|
34
|
+
|
34
35
|
send(:"#{table}_static_record_type=", static_record.class.name)
|
35
36
|
end
|
36
37
|
end
|
@@ -67,14 +67,20 @@ module StaticRecord
|
|
67
67
|
end
|
68
68
|
|
69
69
|
def first(amount = 1)
|
70
|
-
@order_by << { :"#{@primary_key}" => :asc } if @order_by.empty?
|
70
|
+
@order_by << { :"#{@primary_key}" => :asc } if @primary_key && @order_by.empty?
|
71
71
|
take(amount)
|
72
72
|
end
|
73
73
|
|
74
74
|
def last(amount = 1)
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
if !@primary_key && @order_by.empty?
|
76
|
+
cnt = self.class.new(self, store: @store, primary_key: @primary_key).no_sql.send(:count)
|
77
|
+
@sql_offset = [cnt - amount, 0].max
|
78
|
+
res = take(amount)
|
79
|
+
else
|
80
|
+
@order_by << { :"#{@primary_key}" => :desc } if @order_by.empty?
|
81
|
+
res = take(amount)
|
82
|
+
res.reverse! if res.is_a?(Array)
|
83
|
+
end
|
78
84
|
res
|
79
85
|
end
|
80
86
|
|
@@ -107,6 +113,11 @@ module StaticRecord
|
|
107
113
|
self
|
108
114
|
end
|
109
115
|
|
116
|
+
def no_sql
|
117
|
+
@only_sql = false
|
118
|
+
self
|
119
|
+
end
|
120
|
+
|
110
121
|
def to_a
|
111
122
|
exec_request
|
112
123
|
end
|
@@ -85,6 +85,11 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
85
85
|
expect(Article.last.class).to eql(ArticleTwo)
|
86
86
|
expect(Article.see_sql_of.last).to eql("SELECT * FROM articles ORDER BY articles.name DESC LIMIT 1")
|
87
87
|
end
|
88
|
+
|
89
|
+
it 'returns use default sort when no primary key has been defined' do
|
90
|
+
expect(Role.last.class).to eql(RoleTwo)
|
91
|
+
expect(Role.see_sql_of.last).to eql("SELECT * FROM roles LIMIT 1 OFFSET #{Role.all.count - 1}")
|
92
|
+
end
|
88
93
|
end
|
89
94
|
|
90
95
|
context 'with a parameter' do
|
@@ -92,6 +97,11 @@ RSpec.describe StaticRecord::Relation, :type => :model do
|
|
92
97
|
expect(Article.last(2).map(&:class)).to eql([ArticleThree, ArticleTwo])
|
93
98
|
expect(Article.see_sql_of.last(2)).to eql("SELECT * FROM articles ORDER BY articles.name DESC LIMIT 2")
|
94
99
|
end
|
100
|
+
|
101
|
+
it 'returns up to specified number of records from the end when no primary key is set' do
|
102
|
+
expect(Role.last(2).map(&:class)).to eql([RoleOne, RoleTwo])
|
103
|
+
expect(Role.see_sql_of.last(2)).to eql("SELECT * FROM roles LIMIT 2 OFFSET #{Role.all.count - 2}")
|
104
|
+
end
|
95
105
|
end
|
96
106
|
end
|
97
107
|
|
@@ -15,8 +15,13 @@ RSpec.describe StaticRecord::HasStaticRecord, :type => :module do
|
|
15
15
|
expect(t.article.name).to eql(article.name)
|
16
16
|
end
|
17
17
|
|
18
|
+
it 'must assign correct instance' do
|
19
|
+
expect { Test.new.article = Article.last }.not_to raise_error
|
20
|
+
expect { Test.new.article = Role.last }.to raise_error(StaticRecord::ClassError)
|
21
|
+
end
|
22
|
+
|
18
23
|
it 'cannot add getter to ActiveRecord if no primary key is set' do
|
19
24
|
Test.has_static_record :role
|
20
|
-
expect { Test.new.role = Role.last }.to raise_error
|
25
|
+
expect { Test.new.role = Role.last }.to raise_error(StaticRecord::NoPrimaryKey)
|
21
26
|
end
|
22
27
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static-record
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hugo Chevalier
|
@@ -173,6 +173,7 @@ files:
|
|
173
173
|
- spec/test_app/app/models/articles/article_two.rb
|
174
174
|
- spec/test_app/app/models/role.rb
|
175
175
|
- spec/test_app/app/models/roles/role_one.rb
|
176
|
+
- spec/test_app/app/models/roles/role_two.rb
|
176
177
|
- spec/test_app/app/models/test.rb
|
177
178
|
- spec/test_app/config/application.rb
|
178
179
|
- spec/test_app/config/boot.rb
|
@@ -209,9 +210,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
209
210
|
version: '0'
|
210
211
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
211
212
|
requirements:
|
212
|
-
- - "
|
213
|
+
- - ">="
|
213
214
|
- !ruby/object:Gem::Version
|
214
|
-
version:
|
215
|
+
version: '0'
|
215
216
|
requirements: []
|
216
217
|
rubyforge_project:
|
217
218
|
rubygems_version: 2.4.8
|
@@ -235,6 +236,7 @@ test_files:
|
|
235
236
|
- spec/test_app/app/models/articles/article_two.rb
|
236
237
|
- spec/test_app/app/models/role.rb
|
237
238
|
- spec/test_app/app/models/roles/role_one.rb
|
239
|
+
- spec/test_app/app/models/roles/role_two.rb
|
238
240
|
- spec/test_app/app/models/test.rb
|
239
241
|
- spec/test_app/config/application.rb
|
240
242
|
- spec/test_app/config/boot.rb
|