baza_models 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +2 -1
- data/Gemfile.lock +7 -3
- data/VERSION +1 -1
- data/baza_models.gemspec +13 -6
- data/lib/baza_models/model/active_record_column_adapater.rb +32 -0
- data/lib/baza_models/model/has_many_relations.rb +2 -2
- data/lib/baza_models/model/has_one_relations.rb +2 -2
- data/lib/baza_models/model/manipulation.rb +1 -1
- data/lib/baza_models/model/reflection.rb +25 -0
- data/lib/baza_models/model/scopes.rb +1 -1
- data/lib/baza_models/model.rb +35 -3
- data/lib/baza_models/query/pagination.rb +54 -0
- data/lib/baza_models/query/sql_generator.rb +1 -1
- data/lib/baza_models/query.rb +82 -83
- data/lib/baza_models/ransacker/relationship_scanner.rb +30 -14
- data/lib/baza_models/ransacker.rb +9 -15
- data/spec/baza_models/baza_orm_adapter_spec.rb +1 -1
- data/spec/baza_models/model_spec.rb +45 -0
- data/spec/baza_models/query/pagination_spec.rb +64 -0
- data/spec/baza_models/ransacker_spec.rb +54 -2
- data/spec/support/database_helper.rb +77 -70
- data/spec/test_classes/user.rb +5 -0
- metadata +22 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 24043c37709619d4fa62e93c29aca64d38343f52
|
|
4
|
+
data.tar.gz: 3f314d8915ee7c914cd6c3d7831a5719c66d965d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3936eb5b728fa94d82cf3703027ef015c8c910328d6d912994ad27cf91a62a8bfbfd58f9235b383917c78bae607def2f206a263cf8c59d41e4a19db0310da279
|
|
7
|
+
data.tar.gz: 74070bf381abb76c1d46a0e2542321023b6fb047e08387dd839acf2b31141deb17108c35770ca19c37139af5596248963df82e0b76107debce70a29f06a53c4d
|
data/Gemfile
CHANGED
|
@@ -4,10 +4,11 @@ source "http://rubygems.org"
|
|
|
4
4
|
# gem "activesupport", ">= 2.3.5"
|
|
5
5
|
|
|
6
6
|
gem "array_enumerator", ">= 0.0.10"
|
|
7
|
-
gem "baza", "~> 0.0.
|
|
7
|
+
gem "baza", "~> 0.0.21"
|
|
8
8
|
gem "string-cases", ">= 0.0.3"
|
|
9
9
|
gem "auto_autoloader", ">= 0.0.1"
|
|
10
10
|
gem "html_gen", ">= 0.0.12", require: false
|
|
11
|
+
gem "simple_delegate", ">= 0.0.2"
|
|
11
12
|
|
|
12
13
|
# Add dependencies to develop your gem here.
|
|
13
14
|
# Include everything needed to run rake, tests, features, etc.
|
data/Gemfile.lock
CHANGED
|
@@ -19,11 +19,13 @@ GEM
|
|
|
19
19
|
ast (2.2.0)
|
|
20
20
|
auto_autoloader (0.0.4)
|
|
21
21
|
string-cases
|
|
22
|
-
baza (0.0.
|
|
22
|
+
baza (0.0.21)
|
|
23
23
|
array_enumerator (~> 0.0.10)
|
|
24
|
+
auto_autoloader (~> 0.0.1)
|
|
24
25
|
datet (~> 0.0.25)
|
|
25
26
|
event_handler (~> 0.0.0)
|
|
26
|
-
|
|
27
|
+
simple_delegate (~> 0.0.2)
|
|
28
|
+
string-cases (~> 0.0.4)
|
|
27
29
|
wref (~> 0.0.8)
|
|
28
30
|
builder (3.2.2)
|
|
29
31
|
codeclimate-test-reporter (0.4.7)
|
|
@@ -108,6 +110,7 @@ GEM
|
|
|
108
110
|
ruby-progressbar (~> 1.7)
|
|
109
111
|
unicode-display_width (~> 0.3)
|
|
110
112
|
ruby-progressbar (1.7.5)
|
|
113
|
+
simple_delegate (0.0.2)
|
|
111
114
|
simplecov (0.10.0)
|
|
112
115
|
docile (~> 1.1.0)
|
|
113
116
|
json (~> 1.8)
|
|
@@ -129,7 +132,7 @@ PLATFORMS
|
|
|
129
132
|
DEPENDENCIES
|
|
130
133
|
array_enumerator (>= 0.0.10)
|
|
131
134
|
auto_autoloader (>= 0.0.1)
|
|
132
|
-
baza (~> 0.0.
|
|
135
|
+
baza (~> 0.0.21)
|
|
133
136
|
best_practice_project!
|
|
134
137
|
bundler (~> 1.0)
|
|
135
138
|
codeclimate-test-reporter
|
|
@@ -141,6 +144,7 @@ DEPENDENCIES
|
|
|
141
144
|
rdoc (~> 3.12)
|
|
142
145
|
rspec (~> 3.3.0)
|
|
143
146
|
rubocop (= 0.37.0)
|
|
147
|
+
simple_delegate (>= 0.0.2)
|
|
144
148
|
sqlite3
|
|
145
149
|
string-cases (>= 0.0.3)
|
|
146
150
|
|
data/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
0.0.
|
|
1
|
+
0.0.6
|
data/baza_models.gemspec
CHANGED
|
@@ -2,16 +2,16 @@
|
|
|
2
2
|
# DO NOT EDIT THIS FILE DIRECTLY
|
|
3
3
|
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
|
4
4
|
# -*- encoding: utf-8 -*-
|
|
5
|
-
# stub: baza_models 0.0.
|
|
5
|
+
# stub: baza_models 0.0.6 ruby lib
|
|
6
6
|
|
|
7
7
|
Gem::Specification.new do |s|
|
|
8
8
|
s.name = "baza_models"
|
|
9
|
-
s.version = "0.0.
|
|
9
|
+
s.version = "0.0.6"
|
|
10
10
|
|
|
11
11
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
|
12
12
|
s.require_paths = ["lib"]
|
|
13
13
|
s.authors = ["kaspernj"]
|
|
14
|
-
s.date = "2016-04-
|
|
14
|
+
s.date = "2016-04-11"
|
|
15
15
|
s.description = "ActiveRecord like models for the Baza database framework"
|
|
16
16
|
s.email = "k@spernj.org"
|
|
17
17
|
s.extra_rdoc_files = [
|
|
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
|
|
|
39
39
|
"lib/baza_models/helpers.rb",
|
|
40
40
|
"lib/baza_models/helpers/ransacker_helper.rb",
|
|
41
41
|
"lib/baza_models/model.rb",
|
|
42
|
+
"lib/baza_models/model/active_record_column_adapater.rb",
|
|
42
43
|
"lib/baza_models/model/belongs_to_relations.rb",
|
|
43
44
|
"lib/baza_models/model/custom_validations.rb",
|
|
44
45
|
"lib/baza_models/model/delegation.rb",
|
|
@@ -46,12 +47,14 @@ Gem::Specification.new do |s|
|
|
|
46
47
|
"lib/baza_models/model/has_one_relations.rb",
|
|
47
48
|
"lib/baza_models/model/manipulation.rb",
|
|
48
49
|
"lib/baza_models/model/queries.rb",
|
|
50
|
+
"lib/baza_models/model/reflection.rb",
|
|
49
51
|
"lib/baza_models/model/scopes.rb",
|
|
50
52
|
"lib/baza_models/model/translation_functionality.rb",
|
|
51
53
|
"lib/baza_models/model/validations.rb",
|
|
52
54
|
"lib/baza_models/query.rb",
|
|
53
55
|
"lib/baza_models/query/inspector.rb",
|
|
54
56
|
"lib/baza_models/query/not.rb",
|
|
57
|
+
"lib/baza_models/query/pagination.rb",
|
|
55
58
|
"lib/baza_models/query/sql_generator.rb",
|
|
56
59
|
"lib/baza_models/ransacker.rb",
|
|
57
60
|
"lib/baza_models/ransacker/relationship_scanner.rb",
|
|
@@ -80,6 +83,7 @@ Gem::Specification.new do |s|
|
|
|
80
83
|
"spec/baza_models/model/validations_spec.rb",
|
|
81
84
|
"spec/baza_models/model_spec.rb",
|
|
82
85
|
"spec/baza_models/query/not_spec.rb",
|
|
86
|
+
"spec/baza_models/query/pagination_spec.rb",
|
|
83
87
|
"spec/baza_models/query_spec.rb",
|
|
84
88
|
"spec/baza_models/ransacker_spec.rb",
|
|
85
89
|
"spec/baza_models/validators/confirmation_validator_spec.rb",
|
|
@@ -107,10 +111,11 @@ Gem::Specification.new do |s|
|
|
|
107
111
|
|
|
108
112
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
|
109
113
|
s.add_runtime_dependency(%q<array_enumerator>, [">= 0.0.10"])
|
|
110
|
-
s.add_runtime_dependency(%q<baza>, ["~> 0.0.
|
|
114
|
+
s.add_runtime_dependency(%q<baza>, ["~> 0.0.21"])
|
|
111
115
|
s.add_runtime_dependency(%q<string-cases>, [">= 0.0.3"])
|
|
112
116
|
s.add_runtime_dependency(%q<auto_autoloader>, [">= 0.0.1"])
|
|
113
117
|
s.add_runtime_dependency(%q<html_gen>, [">= 0.0.12"])
|
|
118
|
+
s.add_runtime_dependency(%q<simple_delegate>, [">= 0.0.2"])
|
|
114
119
|
s.add_development_dependency(%q<rspec>, ["~> 3.3.0"])
|
|
115
120
|
s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
|
|
116
121
|
s.add_development_dependency(%q<bundler>, ["~> 1.0"])
|
|
@@ -123,10 +128,11 @@ Gem::Specification.new do |s|
|
|
|
123
128
|
s.add_development_dependency(%q<orm_adapter>, [">= 0"])
|
|
124
129
|
else
|
|
125
130
|
s.add_dependency(%q<array_enumerator>, [">= 0.0.10"])
|
|
126
|
-
s.add_dependency(%q<baza>, ["~> 0.0.
|
|
131
|
+
s.add_dependency(%q<baza>, ["~> 0.0.21"])
|
|
127
132
|
s.add_dependency(%q<string-cases>, [">= 0.0.3"])
|
|
128
133
|
s.add_dependency(%q<auto_autoloader>, [">= 0.0.1"])
|
|
129
134
|
s.add_dependency(%q<html_gen>, [">= 0.0.12"])
|
|
135
|
+
s.add_dependency(%q<simple_delegate>, [">= 0.0.2"])
|
|
130
136
|
s.add_dependency(%q<rspec>, ["~> 3.3.0"])
|
|
131
137
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
|
132
138
|
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
|
@@ -140,10 +146,11 @@ Gem::Specification.new do |s|
|
|
|
140
146
|
end
|
|
141
147
|
else
|
|
142
148
|
s.add_dependency(%q<array_enumerator>, [">= 0.0.10"])
|
|
143
|
-
s.add_dependency(%q<baza>, ["~> 0.0.
|
|
149
|
+
s.add_dependency(%q<baza>, ["~> 0.0.21"])
|
|
144
150
|
s.add_dependency(%q<string-cases>, [">= 0.0.3"])
|
|
145
151
|
s.add_dependency(%q<auto_autoloader>, [">= 0.0.1"])
|
|
146
152
|
s.add_dependency(%q<html_gen>, [">= 0.0.12"])
|
|
153
|
+
s.add_dependency(%q<simple_delegate>, [">= 0.0.2"])
|
|
147
154
|
s.add_dependency(%q<rspec>, ["~> 3.3.0"])
|
|
148
155
|
s.add_dependency(%q<rdoc>, ["~> 3.12"])
|
|
149
156
|
s.add_dependency(%q<bundler>, ["~> 1.0"])
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
class BazaModels::Model::ActiveRecordColumnAdapater
|
|
2
|
+
def initialize(baza_column)
|
|
3
|
+
@baza_column = baza_column
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def name
|
|
7
|
+
@baza_column.name
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def null
|
|
11
|
+
@baza_column.null?
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def sql_type
|
|
15
|
+
result = @baza_column.type.to_s.clone
|
|
16
|
+
result << "(#{@baza_column.maxlength})" if @baza_column.maxlength
|
|
17
|
+
result
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def type
|
|
21
|
+
case @baza_column.type
|
|
22
|
+
when :int
|
|
23
|
+
:integer
|
|
24
|
+
when :tinyint
|
|
25
|
+
:boolean
|
|
26
|
+
when :varchar, :string, :text
|
|
27
|
+
:string
|
|
28
|
+
else
|
|
29
|
+
raise "Unknown type: #{@baza_column.type}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -41,8 +41,8 @@ module BazaModels::Model::HasManyRelations
|
|
|
41
41
|
define_method(relation_name) do
|
|
42
42
|
class_instance = StringCases.constantize(relation.fetch(:class_name))
|
|
43
43
|
query = class_instance.where(relation.fetch(:foreign_key) => id)
|
|
44
|
-
query.
|
|
45
|
-
query.
|
|
44
|
+
query.previous_model = self
|
|
45
|
+
query.relation = relation
|
|
46
46
|
|
|
47
47
|
all_args.each do |arg|
|
|
48
48
|
query = query.instance_exec(&arg) if arg.is_a?(Proc)
|
|
@@ -46,8 +46,8 @@ module BazaModels::Model::HasOneRelations
|
|
|
46
46
|
class_instance = StringCases.constantize(relation.fetch(:class_name))
|
|
47
47
|
|
|
48
48
|
query = class_instance.where(relation.fetch(:foreign_key) => id)
|
|
49
|
-
query.
|
|
50
|
-
query.
|
|
49
|
+
query.previous_model = self
|
|
50
|
+
query.relation = relation
|
|
51
51
|
|
|
52
52
|
all_args.each do |arg|
|
|
53
53
|
query = query.instance_exec(&arg) if arg.is_a?(Proc)
|
|
@@ -40,7 +40,7 @@ module BazaModels::Model::Manipulation
|
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
fire_callbacks(:before_create)
|
|
43
|
-
self.created_at
|
|
43
|
+
self.created_at ||= Time.now if has_attribute?(:created_at)
|
|
44
44
|
|
|
45
45
|
@data[:id] = db.insert(table_name, @changes, return_id: true)
|
|
46
46
|
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
class BazaModels::Model::Reflection
|
|
2
|
+
def initialize(relationship)
|
|
3
|
+
@relationship = relationship
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
def class_name
|
|
7
|
+
@relationship.fetch(:class_name)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def collection?
|
|
11
|
+
@relationship.fetch(:type) == :has_many
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def foreign_key
|
|
15
|
+
@relationship.fetch(:foreign_key).to_s
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def klass
|
|
19
|
+
StringCases.constantize(class_name)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def name
|
|
23
|
+
@relationship.fetch(:relation_name)
|
|
24
|
+
end
|
|
25
|
+
end
|
data/lib/baza_models/model.rb
CHANGED
|
@@ -91,6 +91,11 @@ class BazaModels::Model
|
|
|
91
91
|
|
|
92
92
|
attr_writer :db
|
|
93
93
|
|
|
94
|
+
def self.attribute_names
|
|
95
|
+
init_model
|
|
96
|
+
@table.columns.map(&:name).map(&:clone)
|
|
97
|
+
end
|
|
98
|
+
|
|
94
99
|
def self.db
|
|
95
100
|
@db = nil if @db && @db.closed?
|
|
96
101
|
return @db if @db
|
|
@@ -107,6 +112,33 @@ class BazaModels::Model
|
|
|
107
112
|
@db.transaction(&blk)
|
|
108
113
|
end
|
|
109
114
|
|
|
115
|
+
def self.columns
|
|
116
|
+
init_model
|
|
117
|
+
@table.columns.map do |column|
|
|
118
|
+
BazaModels::Model::ActiveRecordColumnAdapater.new(column)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.columns_hash
|
|
123
|
+
init_model
|
|
124
|
+
result = {}
|
|
125
|
+
|
|
126
|
+
@table.columns do |column|
|
|
127
|
+
result[column.name] = BazaModels::Model::ActiveRecordColumnAdapater.new(column)
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
result
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def self.reflections
|
|
134
|
+
result = {}
|
|
135
|
+
relationships.each_value do |relationship|
|
|
136
|
+
result[relationship.fetch(:relation_name).to_s] = BazaModels::Model::Reflection.new(relationship)
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
result
|
|
140
|
+
end
|
|
141
|
+
|
|
110
142
|
class << self
|
|
111
143
|
attr_writer :db, :table_name
|
|
112
144
|
end
|
|
@@ -134,15 +166,15 @@ class BazaModels::Model
|
|
|
134
166
|
self
|
|
135
167
|
end
|
|
136
168
|
|
|
137
|
-
def self.init_model
|
|
138
|
-
return if @model_initialized
|
|
169
|
+
def self.init_model(args = {})
|
|
170
|
+
return if @model_initialized && !args[:force]
|
|
139
171
|
|
|
140
172
|
@table = db.tables[table_name]
|
|
141
173
|
|
|
142
174
|
@__blank_attributes ||= {}
|
|
143
175
|
|
|
144
176
|
@table.columns do |column|
|
|
145
|
-
init_attribute_from_column(column)
|
|
177
|
+
init_attribute_from_column(column) unless @model_initialized
|
|
146
178
|
@__blank_attributes[column.name.to_sym] = nil
|
|
147
179
|
end
|
|
148
180
|
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
module BazaModels::Query::Pagination
|
|
2
|
+
def current_page
|
|
3
|
+
if @page
|
|
4
|
+
@page
|
|
5
|
+
else
|
|
6
|
+
raise "Page has not been set"
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def out_of_bounds?
|
|
11
|
+
current_page > total_pages
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def page(some_page = :non_given)
|
|
15
|
+
if some_page == :non_given
|
|
16
|
+
@page ||= 1
|
|
17
|
+
else
|
|
18
|
+
some_page ||= 1
|
|
19
|
+
some_page = some_page.to_i
|
|
20
|
+
offset = (some_page - 1) * per
|
|
21
|
+
|
|
22
|
+
clone(page: some_page, offset: offset, limit: per)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def paginated?
|
|
27
|
+
@page != nil
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def per(value = :non_given)
|
|
31
|
+
if value == :non_given
|
|
32
|
+
@per ||= 30
|
|
33
|
+
else
|
|
34
|
+
value = value.to_i
|
|
35
|
+
offset = (page - 1) * value
|
|
36
|
+
clone(limit: value, offset: offset, per: value)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
alias per_page per
|
|
41
|
+
|
|
42
|
+
def total_entries
|
|
43
|
+
@model.count
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def total_pages
|
|
47
|
+
pages_count = (count.to_f / per.to_f)
|
|
48
|
+
|
|
49
|
+
pages_count = 1 if pages_count.nan? || pages_count == Float::INFINITY
|
|
50
|
+
pages_count = pages_count.ceil
|
|
51
|
+
pages_count = 1 if pages_count == 0
|
|
52
|
+
pages_count
|
|
53
|
+
end
|
|
54
|
+
end
|
|
@@ -3,7 +3,7 @@ class BazaModels::Query::SqlGenerator
|
|
|
3
3
|
@query = args.fetch(:query)
|
|
4
4
|
|
|
5
5
|
instance_variables = [
|
|
6
|
-
:selects, :joins, :wheres, :groups, :orders, :limit, :offset,
|
|
6
|
+
:selects, :joins, :wheres, :groups, :orders, :per, :limit, :offset,
|
|
7
7
|
:model, :table_name, :reverse_order
|
|
8
8
|
]
|
|
9
9
|
instance_variables.each do |instance_variable|
|
data/lib/baza_models/query.rb
CHANGED
|
@@ -3,7 +3,9 @@ require "array_enumerator"
|
|
|
3
3
|
class BazaModels::Query
|
|
4
4
|
AutoAutoloader.autoload_sub_classes(self, __FILE__)
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
include BazaModels::Query::Pagination
|
|
7
|
+
|
|
8
|
+
attr_accessor :previous_model, :relation
|
|
7
9
|
|
|
8
10
|
def initialize(args)
|
|
9
11
|
@args = args
|
|
@@ -17,7 +19,11 @@ class BazaModels::Query
|
|
|
17
19
|
@includes = args[:includes] || []
|
|
18
20
|
@joins = args[:joins] || []
|
|
19
21
|
@groups = args[:groups] || []
|
|
22
|
+
@offset = args[:offset]
|
|
20
23
|
@orders = args[:orders] || []
|
|
24
|
+
@page = args[:page]
|
|
25
|
+
@per = args[:per]
|
|
26
|
+
@previous_model = args[:previous_model]
|
|
21
27
|
@limit = args[:limit]
|
|
22
28
|
|
|
23
29
|
@joins_tracker = {}
|
|
@@ -29,9 +35,9 @@ class BazaModels::Query
|
|
|
29
35
|
|
|
30
36
|
def any?
|
|
31
37
|
if @db.query(clone.select(:id).limit(1).to_sql).fetch
|
|
32
|
-
|
|
38
|
+
true
|
|
33
39
|
else
|
|
34
|
-
|
|
40
|
+
false
|
|
35
41
|
end
|
|
36
42
|
end
|
|
37
43
|
|
|
@@ -44,21 +50,21 @@ class BazaModels::Query
|
|
|
44
50
|
end
|
|
45
51
|
|
|
46
52
|
def count
|
|
47
|
-
if @
|
|
48
|
-
|
|
53
|
+
if @previous_model && @previous_model.new_record?
|
|
54
|
+
autoloaded_cache_or_create.length
|
|
49
55
|
else
|
|
50
|
-
query = clone
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
query = clone(selects: [])
|
|
57
|
+
.select("COUNT(*) AS count")
|
|
58
|
+
.limit(nil)
|
|
59
|
+
.offset(nil)
|
|
54
60
|
|
|
55
61
|
@db.query(query.to_sql).fetch.fetch(:count)
|
|
56
62
|
end
|
|
57
63
|
end
|
|
58
64
|
|
|
59
65
|
def length
|
|
60
|
-
if @
|
|
61
|
-
@
|
|
66
|
+
if @previous_model && !any_wheres_other_than_relation? && @previous_model.autoloads[@relation.fetch(:relation_name)]
|
|
67
|
+
@previous_model.autoloads[@relation.fetch(:relation_name)].length
|
|
62
68
|
else
|
|
63
69
|
count
|
|
64
70
|
end
|
|
@@ -70,10 +76,10 @@ class BazaModels::Query
|
|
|
70
76
|
end
|
|
71
77
|
|
|
72
78
|
def new(attributes)
|
|
73
|
-
raise "No previous model" unless @
|
|
74
|
-
raise "No relation" unless @
|
|
79
|
+
raise "No previous model" unless @previous_model
|
|
80
|
+
raise "No relation" unless @relation
|
|
75
81
|
|
|
76
|
-
new_sub_model = @model.new(@
|
|
82
|
+
new_sub_model = @model.new(@relation.fetch(:foreign_key) => @previous_model.id)
|
|
77
83
|
new_sub_model.assign_attributes(attributes)
|
|
78
84
|
autoloaded_cache_or_create << new_sub_model
|
|
79
85
|
|
|
@@ -121,39 +127,41 @@ class BazaModels::Query
|
|
|
121
127
|
def select(select = nil, &blk)
|
|
122
128
|
if !select && blk
|
|
123
129
|
to_enum.select(&blk)
|
|
130
|
+
elsif select.is_a?(Symbol)
|
|
131
|
+
clone(selects: @selects + ["`#{@model.table_name}`.`#{select}`"])
|
|
124
132
|
else
|
|
125
|
-
|
|
126
|
-
@selects << "`#{@model.table_name}`.`#{select}`"
|
|
127
|
-
else
|
|
128
|
-
@selects << select
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
self
|
|
133
|
+
clone(selects: @selects + [select])
|
|
132
134
|
end
|
|
133
135
|
end
|
|
134
136
|
|
|
135
137
|
def offset(offset)
|
|
136
|
-
|
|
137
|
-
self
|
|
138
|
+
clone(offset: offset)
|
|
138
139
|
end
|
|
139
140
|
|
|
140
141
|
def limit(limit)
|
|
141
|
-
|
|
142
|
-
self
|
|
142
|
+
clone(limit: limit)
|
|
143
143
|
end
|
|
144
144
|
|
|
145
|
-
def includes(
|
|
146
|
-
@includes
|
|
147
|
-
self
|
|
145
|
+
def includes(*names)
|
|
146
|
+
clone(includes: @includes + names)
|
|
148
147
|
end
|
|
149
148
|
|
|
150
|
-
def where(args
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
149
|
+
def where(*args)
|
|
150
|
+
first_arg = args.first
|
|
151
|
+
new_wheres = @wheres.dup
|
|
152
|
+
|
|
153
|
+
if first_arg.is_a?(String)
|
|
154
|
+
new_where = "(#{args.shift})"
|
|
155
155
|
|
|
156
156
|
args.each do |arg|
|
|
157
|
+
new_where.sub!("?", @db.sqlval(arg))
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
new_wheres << new_where
|
|
161
|
+
elsif first_arg.is_a?(Array)
|
|
162
|
+
str = first_arg.shift
|
|
163
|
+
|
|
164
|
+
first_arg.each do |arg|
|
|
157
165
|
if arg.is_a?(Symbol)
|
|
158
166
|
arg = "`#{@model.table_name}`.`#{@db.escape_column(arg)}`"
|
|
159
167
|
elsif arg.is_a?(FalseClass)
|
|
@@ -167,22 +175,22 @@ class BazaModels::Query
|
|
|
167
175
|
str.sub!("?", arg)
|
|
168
176
|
end
|
|
169
177
|
|
|
170
|
-
|
|
171
|
-
elsif
|
|
178
|
+
new_wheres << "(#{str})"
|
|
179
|
+
elsif first_arg == nil
|
|
172
180
|
return Not.new(query: self)
|
|
173
181
|
else
|
|
174
|
-
|
|
182
|
+
first_arg.each do |key, value|
|
|
175
183
|
if value.is_a?(Hash)
|
|
176
184
|
value.each do |hash_key, hash_value|
|
|
177
|
-
|
|
185
|
+
new_wheres << "`#{key}`.`#{key_convert(hash_key, hash_value)}` #{value_with_mode(value_convert(hash_value))}"
|
|
178
186
|
end
|
|
179
187
|
else
|
|
180
|
-
|
|
188
|
+
new_wheres << "`#{@model.table_name}`.`#{key_convert(key, value)}` #{value_with_mode(value_convert(value))}"
|
|
181
189
|
end
|
|
182
190
|
end
|
|
183
191
|
end
|
|
184
192
|
|
|
185
|
-
|
|
193
|
+
clone(wheres: new_wheres)
|
|
186
194
|
end
|
|
187
195
|
|
|
188
196
|
def joins(*arguments)
|
|
@@ -199,26 +207,22 @@ class BazaModels::Query
|
|
|
199
207
|
|
|
200
208
|
def group(name)
|
|
201
209
|
if name.is_a?(Symbol)
|
|
202
|
-
@groups
|
|
210
|
+
clone(groups: @groups + ["`#{@model.table_name}`.`#{name}`"])
|
|
203
211
|
elsif name.is_a?(String)
|
|
204
|
-
@groups
|
|
212
|
+
clone(groups: @groups + [name])
|
|
205
213
|
else
|
|
206
214
|
raise "Didn't know how to group by that argument: #{name}"
|
|
207
215
|
end
|
|
208
|
-
|
|
209
|
-
self
|
|
210
216
|
end
|
|
211
217
|
|
|
212
218
|
def order(name)
|
|
213
219
|
if name.is_a?(Symbol)
|
|
214
|
-
@orders
|
|
220
|
+
clone(orders: @orders + ["`#{@model.table_name}`.`#{name}`"])
|
|
215
221
|
elsif name.is_a?(String)
|
|
216
|
-
@orders
|
|
222
|
+
clone(orders: @orders + [name])
|
|
217
223
|
else
|
|
218
224
|
raise "Didn't know how to order by that argument: #{name}"
|
|
219
225
|
end
|
|
220
|
-
|
|
221
|
-
self
|
|
222
226
|
end
|
|
223
227
|
|
|
224
228
|
def reverse_order
|
|
@@ -310,11 +314,11 @@ class BazaModels::Query
|
|
|
310
314
|
end
|
|
311
315
|
|
|
312
316
|
def <<(model)
|
|
313
|
-
raise "No previous model set" unless @
|
|
314
|
-
raise "No relation" unless @
|
|
317
|
+
raise "No previous model set" unless @previous_model
|
|
318
|
+
raise "No relation" unless @relation
|
|
315
319
|
|
|
316
320
|
if model.persisted?
|
|
317
|
-
model.update_attributes!(@
|
|
321
|
+
model.update_attributes!(@relation.fetch(:foreign_key) => @previous_model.id)
|
|
318
322
|
else
|
|
319
323
|
autoloaded_cache_or_create << model
|
|
320
324
|
end
|
|
@@ -336,25 +340,6 @@ class BazaModels::Query
|
|
|
336
340
|
"'#{@db.esc(value)}'"
|
|
337
341
|
end
|
|
338
342
|
|
|
339
|
-
def page(some_page)
|
|
340
|
-
some_page ||= 1
|
|
341
|
-
offset = (some_page.to_i - 1) * per
|
|
342
|
-
|
|
343
|
-
clone.offset(offset).limit(30)
|
|
344
|
-
end
|
|
345
|
-
|
|
346
|
-
def per
|
|
347
|
-
@per ||= 30
|
|
348
|
-
end
|
|
349
|
-
|
|
350
|
-
def total_pages
|
|
351
|
-
pages_count = (count.to_f / @per.to_f)
|
|
352
|
-
pages_count = 1 if pages_count.nan? || pages_count == Float::INFINITY
|
|
353
|
-
pages_count = pages_count.to_i
|
|
354
|
-
pages_count = 1 if pages_count == 0
|
|
355
|
-
pages_count
|
|
356
|
-
end
|
|
357
|
-
|
|
358
343
|
def ransack(params)
|
|
359
344
|
BazaModels::Ransacker.new(class: @model, params: params, query: self)
|
|
360
345
|
end
|
|
@@ -366,12 +351,12 @@ private
|
|
|
366
351
|
end
|
|
367
352
|
|
|
368
353
|
def autoloaded_cache_or_create
|
|
369
|
-
@
|
|
354
|
+
@previous_model.autoloads[@relation.fetch(:relation_name)] ||= []
|
|
370
355
|
autoloaded_cache
|
|
371
356
|
end
|
|
372
357
|
|
|
373
358
|
def autoloaded_cache
|
|
374
|
-
@
|
|
359
|
+
@previous_model.autoloads.fetch(@relation.fetch(:relation_name))
|
|
375
360
|
end
|
|
376
361
|
|
|
377
362
|
def any_mods?
|
|
@@ -379,8 +364,8 @@ private
|
|
|
379
364
|
end
|
|
380
365
|
|
|
381
366
|
def any_wheres_other_than_relation?
|
|
382
|
-
if @
|
|
383
|
-
looks_like = "`#{@
|
|
367
|
+
if @previous_model && @relation && @wheres.length == 1
|
|
368
|
+
looks_like = "`#{@relation.fetch(:table_name)}`.`#{@relation.fetch(:foreign_key)}` = #{@previous_model.id}"
|
|
384
369
|
|
|
385
370
|
return false if @wheres.first == looks_like
|
|
386
371
|
end
|
|
@@ -389,24 +374,28 @@ private
|
|
|
389
374
|
end
|
|
390
375
|
|
|
391
376
|
def autoloaded_on_previous_model?
|
|
392
|
-
if @
|
|
393
|
-
return true if @
|
|
377
|
+
if @previous_model && @relation
|
|
378
|
+
return true if @previous_model.autoloads.include?(@relation.fetch(:relation_name))
|
|
394
379
|
end
|
|
395
380
|
|
|
396
381
|
false
|
|
397
382
|
end
|
|
398
383
|
|
|
399
|
-
def clone
|
|
400
|
-
BazaModels::Query.new(
|
|
384
|
+
def clone(args = {})
|
|
385
|
+
BazaModels::Query.new({
|
|
401
386
|
model: @model,
|
|
402
|
-
selects: @selects
|
|
403
|
-
wheres: @wheres
|
|
387
|
+
selects: @selects,
|
|
388
|
+
wheres: @wheres,
|
|
404
389
|
joins: @joins.dup,
|
|
405
|
-
includes: @includes
|
|
406
|
-
groups: @groups
|
|
407
|
-
|
|
390
|
+
includes: @includes,
|
|
391
|
+
groups: @groups,
|
|
392
|
+
offset: @offset,
|
|
393
|
+
orders: @orders,
|
|
394
|
+
page: @page,
|
|
395
|
+
per: @per,
|
|
396
|
+
previous_model: @previous_model,
|
|
408
397
|
limit: @limit
|
|
409
|
-
)
|
|
398
|
+
}.merge(args))
|
|
410
399
|
end
|
|
411
400
|
|
|
412
401
|
def key_convert(key, value)
|
|
@@ -440,4 +429,14 @@ private
|
|
|
440
429
|
"= #{@db.sqlval(value)}"
|
|
441
430
|
end
|
|
442
431
|
end
|
|
432
|
+
|
|
433
|
+
def method_missing(method_name, *args, &blk)
|
|
434
|
+
return super unless @model
|
|
435
|
+
|
|
436
|
+
scopes = @model.instance_variable_get(:@scopes)
|
|
437
|
+
return super if !scopes || !scopes.key?(method_name)
|
|
438
|
+
|
|
439
|
+
block = scopes.fetch(method_name).fetch(:blk)
|
|
440
|
+
instance_exec(*args, &block)
|
|
441
|
+
end
|
|
443
442
|
end
|
|
@@ -85,26 +85,42 @@ private
|
|
|
85
85
|
hash
|
|
86
86
|
end
|
|
87
87
|
|
|
88
|
-
def
|
|
88
|
+
def add_join_parts
|
|
89
89
|
@ransacker.query = @ransacker.query.joins(join_parts_as_hash) if @join_parts.any?
|
|
90
|
+
end
|
|
90
91
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
92
|
+
def add_filter_to_query(args)
|
|
93
|
+
@column_query = "#{@db.sep_col}#{@db.escape_column(args.fetch(:column_name))}#{@db.sep_col}"
|
|
94
|
+
@table_query = "#{@db.sep_table}#{@db.escape_table(@klass.table_name)}#{@db.sep_table}"
|
|
95
|
+
|
|
96
|
+
case @mode
|
|
97
|
+
when :cont
|
|
98
|
+
return if @value.empty?
|
|
99
|
+
add_query_with_symbol("LIKE", "%#{@klass.db.esc(@value)}%")
|
|
100
|
+
when :eq
|
|
101
|
+
add_query_with_symbol("=")
|
|
102
|
+
when :lt
|
|
103
|
+
add_query_with_symbol("<")
|
|
104
|
+
when :lteq
|
|
105
|
+
add_query_with_symbol("<=")
|
|
106
|
+
when :gt
|
|
107
|
+
add_query_with_symbol(">")
|
|
108
|
+
when :gteq
|
|
109
|
+
add_query_with_symbol(">=")
|
|
110
|
+
when :sort
|
|
111
|
+
add_join_parts
|
|
103
112
|
@ransacker.query = @ransacker
|
|
104
113
|
.query
|
|
105
|
-
.order("#{table_query}.#{column_query} #{@value}")
|
|
114
|
+
.order("#{@table_query}.#{@column_query} #{@value}")
|
|
106
115
|
else
|
|
107
116
|
raise "Unknown mode: #{@mode}"
|
|
108
117
|
end
|
|
109
118
|
end
|
|
119
|
+
|
|
120
|
+
def add_query_with_symbol(symbol, value = @value)
|
|
121
|
+
add_join_parts
|
|
122
|
+
@ransacker.query = @ransacker
|
|
123
|
+
.query
|
|
124
|
+
.where("#{@table_query}.#{@column_query} #{symbol} #{@klass.db.sqlval(value)}")
|
|
125
|
+
end
|
|
110
126
|
end
|
|
@@ -8,6 +8,7 @@ class BazaModels::Ransacker
|
|
|
8
8
|
@klass = args.fetch(:class)
|
|
9
9
|
@db = @klass.db
|
|
10
10
|
@params = args.fetch(:params)
|
|
11
|
+
@_registered_params = @params # Support for SimpleFormRansack
|
|
11
12
|
@query = args.fetch(:query)
|
|
12
13
|
end
|
|
13
14
|
|
|
@@ -22,32 +23,25 @@ private
|
|
|
22
23
|
@add_filters_to_query_executed = true
|
|
23
24
|
return unless @params
|
|
24
25
|
|
|
26
|
+
ransackable_scopes = @klass.ransackable_scopes.map(&:to_s) if @klass.respond_to?(:ransackable_scopes)
|
|
27
|
+
|
|
25
28
|
@params.each do |key, value|
|
|
26
|
-
if (match = key.to_s.match(/\A(.+?)
|
|
27
|
-
|
|
28
|
-
elsif (match = key.to_s.match(/\A(.+?)_cont\Z/))
|
|
29
|
-
filter_cont(match[1], value)
|
|
29
|
+
if (match = key.to_s.match(/\A(.+?)_(cont|eq|lt|lteq|gt|gteq)\Z/))
|
|
30
|
+
filter(match[1], value, match[2])
|
|
30
31
|
elsif key.to_s == "s"
|
|
31
32
|
match = value.to_s.match(/\A([A-z_\d]+)\s+(asc|desc)\Z/)
|
|
32
33
|
raise "Couldn't sort-match: #{value}" unless match
|
|
33
34
|
sort_by(column_name: match[1], sort_mode: match[2])
|
|
35
|
+
elsif ransackable_scopes && ransackable_scopes.include?(key.to_s)
|
|
36
|
+
@query = @query.__send__(key, value)
|
|
34
37
|
end
|
|
35
38
|
end
|
|
36
39
|
end
|
|
37
40
|
|
|
38
|
-
def
|
|
39
|
-
BazaModels::Ransacker::RelationshipScanner.new(
|
|
40
|
-
column_name: column_name,
|
|
41
|
-
mode: :eq,
|
|
42
|
-
ransacker: self,
|
|
43
|
-
value: value
|
|
44
|
-
)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
def filter_cont(column_name, value)
|
|
41
|
+
def filter(column_name, value, mode)
|
|
48
42
|
BazaModels::Ransacker::RelationshipScanner.new(
|
|
49
43
|
column_name: column_name,
|
|
50
|
-
mode:
|
|
44
|
+
mode: mode.to_sym,
|
|
51
45
|
ransacker: self,
|
|
52
46
|
value: value
|
|
53
47
|
)
|
|
@@ -47,6 +47,6 @@ describe BazaModels::Query do
|
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
it "#column_names" do
|
|
50
|
-
expect(User.to_adapter.column_names).to eq %w(id organization_id email email_confirmation created_at updated_at)
|
|
50
|
+
expect(User.to_adapter.column_names).to eq %w(id organization_id email email_confirmation created_at updated_at admin)
|
|
51
51
|
end
|
|
52
52
|
end
|
|
@@ -151,6 +151,51 @@ describe "BazaModels::Model" do
|
|
|
151
151
|
expect(count).to eq 1
|
|
152
152
|
end
|
|
153
153
|
|
|
154
|
+
it "#attribute_names" do
|
|
155
|
+
expect(User.attribute_names).to eq %w(id organization_id email email_confirmation created_at updated_at admin)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
it "#columns" do
|
|
159
|
+
id_column = User.columns.find { |column| column.name == "id" }
|
|
160
|
+
|
|
161
|
+
expect(id_column.name).to eq "id"
|
|
162
|
+
expect(id_column.type).to eq :integer
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
it "#columns_hash" do
|
|
166
|
+
columns_hash = User.columns_hash
|
|
167
|
+
|
|
168
|
+
id_column = columns_hash["id"]
|
|
169
|
+
|
|
170
|
+
expect(id_column.type).to eq :integer
|
|
171
|
+
expect(id_column.name).to eq "id"
|
|
172
|
+
expect(id_column.null).to eq true
|
|
173
|
+
expect(id_column.sql_type).to eq "int"
|
|
174
|
+
|
|
175
|
+
email_column = columns_hash["email"]
|
|
176
|
+
|
|
177
|
+
expect(email_column.type).to eq :string
|
|
178
|
+
expect(email_column.name).to eq "email"
|
|
179
|
+
expect(email_column.sql_type).to eq "varchar(255)"
|
|
180
|
+
|
|
181
|
+
admin_column = columns_hash["admin"]
|
|
182
|
+
|
|
183
|
+
expect(admin_column.type).to eq :boolean
|
|
184
|
+
expect(admin_column.name).to eq "admin"
|
|
185
|
+
expect(admin_column.sql_type).to eq "tinyint"
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
it "#reflections" do
|
|
189
|
+
reflections = User.reflections
|
|
190
|
+
|
|
191
|
+
person_reflection = reflections.values.find { |reflection| reflection.name == :person }
|
|
192
|
+
expect(person_reflection.name).to eq :person
|
|
193
|
+
expect(person_reflection.class_name).to eq "Person"
|
|
194
|
+
expect(person_reflection.foreign_key).to eq "user_id"
|
|
195
|
+
expect(person_reflection.klass).to eq Person
|
|
196
|
+
expect(person_reflection.collection?).to eq false
|
|
197
|
+
end
|
|
198
|
+
|
|
154
199
|
it "doesnt care if initialized data has keys as strings" do
|
|
155
200
|
user = User.new("email" => "test@example.com")
|
|
156
201
|
expect(user.email).to eq "test@example.com"
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require "spec_helper"
|
|
2
|
+
|
|
3
|
+
describe BazaModels::Query::Pagination do
|
|
4
|
+
include DatabaseHelper
|
|
5
|
+
|
|
6
|
+
before do
|
|
7
|
+
User.transaction do
|
|
8
|
+
100.times { |n| User.create! email: "user#{n}@example.com" }
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe "#out_of_bounds?" do
|
|
13
|
+
it "returns the right values under the right conditions" do
|
|
14
|
+
collection = User.all.page(5)
|
|
15
|
+
expect(collection.out_of_bounds?).to eq true
|
|
16
|
+
|
|
17
|
+
collection = User.all.page(4)
|
|
18
|
+
expect(collection.out_of_bounds?).to eq false
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe "#paginated?" do
|
|
23
|
+
it "returns the right values under the right conditions" do
|
|
24
|
+
collection = User.all
|
|
25
|
+
expect(collection.paginated?).to eq false
|
|
26
|
+
|
|
27
|
+
collection = collection.page(3)
|
|
28
|
+
expect(collection.paginated?).to eq true
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
describe "#per #total_pages" do
|
|
33
|
+
it "sets a custom per_page" do
|
|
34
|
+
collection = User
|
|
35
|
+
.all
|
|
36
|
+
.per_page(40)
|
|
37
|
+
|
|
38
|
+
expect(collection.per_page).to eq 40
|
|
39
|
+
expect(collection.per).to eq 40
|
|
40
|
+
expect(collection.to_a.length).to eq 40
|
|
41
|
+
expect(collection.total_pages).to eq 3
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
describe "#page" do
|
|
46
|
+
it "sets the page and returns the correct numbers" do
|
|
47
|
+
collection = User
|
|
48
|
+
.all
|
|
49
|
+
.page(2)
|
|
50
|
+
.per_page(60)
|
|
51
|
+
|
|
52
|
+
expect(collection.page).to eq 2
|
|
53
|
+
expect(collection.to_a.length).to eq 40
|
|
54
|
+
expect(collection.page(1).to_a.length).to eq 60
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
describe "#total_entries" do
|
|
59
|
+
it "returns the correct number" do
|
|
60
|
+
collection = User.where(email: "user5@example.com")
|
|
61
|
+
expect(collection.total_entries).to eq 100
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
@@ -5,8 +5,8 @@ describe BazaModels::Query do
|
|
|
5
5
|
|
|
6
6
|
let!(:organization) { Organization.create!(id: 1, name: "Test organization") }
|
|
7
7
|
let!(:person) { Person.create!(id: 1, user: user) }
|
|
8
|
-
let!(:user) { User.create!(id: 1, organization: organization, email: "test@example.com") }
|
|
9
|
-
let!(:another_user) { User.create!(id: 2, organization: nil, email: "another_user@example.com") }
|
|
8
|
+
let!(:user) { User.create!(id: 1, organization: organization, email: "test@example.com", created_at: "2015-06-17 10:00") }
|
|
9
|
+
let!(:another_user) { User.create!(id: 2, organization: nil, email: "another_user@example.com", created_at: "2015-08-20 6:00") }
|
|
10
10
|
let!(:another_person) { Person.create!(id: 2, user: another_user) }
|
|
11
11
|
|
|
12
12
|
it "eq" do
|
|
@@ -17,6 +17,53 @@ describe BazaModels::Query do
|
|
|
17
17
|
expect(User.ransack(email_cont: "test").result.to_a).to eq [user]
|
|
18
18
|
end
|
|
19
19
|
|
|
20
|
+
describe "lt" do
|
|
21
|
+
it "finds the right models" do
|
|
22
|
+
expect(User.ransack(id_lt: 3).result.to_a).to eq [user, another_user]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it "excludes the right models" do
|
|
26
|
+
expect(User.ransack(id_lt: 2).result.to_a).to eq [user]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe "lteq" do
|
|
31
|
+
it "finds the right models" do
|
|
32
|
+
expect(User.ransack(id_lteq: 2).result.to_a).to eq [user, another_user]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "excludes the right models" do
|
|
36
|
+
expect(User.ransack(id_lteq: 1).result.to_a).to eq [user]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
describe "gt" do
|
|
41
|
+
it "finds the right models" do
|
|
42
|
+
expect(User.ransack(id_gt: 0).result.to_a).to eq [user, another_user]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
it "excludes the right models" do
|
|
46
|
+
expect(User.ransack(id_gt: 1).result.to_a).to eq [another_user]
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
describe "gteq" do
|
|
51
|
+
it "finds the right models" do
|
|
52
|
+
expect(User.ransack(id_gteq: 1).result.to_a).to eq [user, another_user]
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "excludes the right models" do
|
|
56
|
+
expect(User.ransack(id_gteq: 2).result.to_a).to eq [another_user]
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
describe "since" do
|
|
61
|
+
it "finds the right users" do
|
|
62
|
+
query = User.ransack(created_at_since: "2015-08-20").result
|
|
63
|
+
expect(query.to_a).to eq [another_user]
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
20
67
|
it "s" do
|
|
21
68
|
query = User.ransack(s: "email asc")
|
|
22
69
|
|
|
@@ -41,6 +88,11 @@ describe BazaModels::Query do
|
|
|
41
88
|
expect(query.result.to_a).to eq [person]
|
|
42
89
|
end
|
|
43
90
|
|
|
91
|
+
it "ignores empty contains" do
|
|
92
|
+
query = Person.ransack(user_organization_name_cont: "")
|
|
93
|
+
expect(query.result.to_sql).to eq "SELECT `persons`.* FROM `persons`"
|
|
94
|
+
end
|
|
95
|
+
|
|
44
96
|
it "ignores unknown parameters and doesn't raise exceptions" do
|
|
45
97
|
Person.ransack(custom_something: "Test").result
|
|
46
98
|
end
|
|
@@ -1,90 +1,97 @@
|
|
|
1
|
+
require "tempfile"
|
|
2
|
+
require "baza"
|
|
3
|
+
|
|
4
|
+
if RUBY_PLATFORM == "java"
|
|
5
|
+
require "jdbc/sqlite3"
|
|
6
|
+
::Jdbc::SQLite3.load_driver
|
|
7
|
+
else
|
|
8
|
+
require "sqlite3"
|
|
9
|
+
end
|
|
10
|
+
|
|
1
11
|
module DatabaseHelper
|
|
2
12
|
def self.included(base)
|
|
3
13
|
base.instance_eval do
|
|
4
14
|
let(:db) { @db }
|
|
5
15
|
|
|
6
16
|
before do
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
require "tempfile"
|
|
11
|
-
require "baza"
|
|
12
|
-
|
|
13
|
-
if RUBY_PLATFORM == "java"
|
|
14
|
-
require "jdbc/sqlite3"
|
|
15
|
-
::Jdbc::SQLite3.load_driver
|
|
16
|
-
else
|
|
17
|
-
require "sqlite3"
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
tempfile = Tempfile.new(["baza_test", ".sqlite3"])
|
|
21
|
-
path = tempfile.path
|
|
22
|
-
tempfile.close!
|
|
23
|
-
|
|
24
|
-
@db = Baza::Db.new(type: :sqlite3, path: path, index_append_table_name: true, debug: false)
|
|
25
|
-
BazaModels.primary_db = @db
|
|
26
|
-
|
|
27
|
-
@db.tables.create(
|
|
28
|
-
:users,
|
|
29
|
-
columns: [
|
|
30
|
-
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
31
|
-
{name: :organization_id, type: :int},
|
|
32
|
-
{name: :email, type: :varchar},
|
|
33
|
-
{name: :email_confirmation, type: :varchar},
|
|
34
|
-
{name: :created_at, type: :datetime},
|
|
35
|
-
{name: :updated_at, type: :datetime}
|
|
36
|
-
],
|
|
37
|
-
indexes: [:organization_id, :email])
|
|
38
|
-
|
|
39
|
-
@db.tables.create(
|
|
40
|
-
:user_passports,
|
|
41
|
-
columns: [
|
|
42
|
-
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
43
|
-
{name: :user_id, type: :int},
|
|
44
|
-
{name: :code, type: :varchar}
|
|
45
|
-
],
|
|
46
|
-
indexes: [
|
|
47
|
-
:user_id
|
|
48
|
-
])
|
|
49
|
-
|
|
50
|
-
@db.tables.create(
|
|
51
|
-
:persons,
|
|
52
|
-
columns: [
|
|
53
|
-
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
54
|
-
{name: :user_id, type: :int}
|
|
55
|
-
],
|
|
56
|
-
indexes: [:user_id])
|
|
17
|
+
init_database_connection
|
|
18
|
+
init_database_structure
|
|
57
19
|
|
|
58
|
-
|
|
59
|
-
:roles,
|
|
60
|
-
columns: [
|
|
61
|
-
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
62
|
-
{name: :user_id, type: :int},
|
|
63
|
-
{name: :role, type: :varchar},
|
|
64
|
-
{name: :created_at, type: :datetime},
|
|
65
|
-
{name: :updated_at, type: :datetime}
|
|
66
|
-
],
|
|
67
|
-
indexes: [:user_id]
|
|
68
|
-
)
|
|
69
|
-
|
|
70
|
-
@db.tables.create(
|
|
71
|
-
:organizations,
|
|
72
|
-
columns: [
|
|
73
|
-
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
74
|
-
{name: :name, type: :varchar}
|
|
75
|
-
]
|
|
76
|
-
)
|
|
20
|
+
User.init_model(force: true)
|
|
77
21
|
end
|
|
78
22
|
|
|
79
23
|
after do
|
|
80
24
|
BazaModels.primary_db = nil
|
|
81
25
|
|
|
82
26
|
@db.close
|
|
83
|
-
path = db.args
|
|
27
|
+
path = db.args.fetch(:path)
|
|
84
28
|
File.unlink(path) if File.exist?(path)
|
|
85
29
|
Thread.current[:baza] = nil
|
|
86
30
|
@db = nil
|
|
87
31
|
end
|
|
88
32
|
end
|
|
89
33
|
end
|
|
34
|
+
|
|
35
|
+
def init_database_connection
|
|
36
|
+
tempfile = Tempfile.new(["baza_test", ".sqlite3"])
|
|
37
|
+
path = tempfile.path
|
|
38
|
+
tempfile.close!
|
|
39
|
+
|
|
40
|
+
@db = Baza::Db.new(type: :sqlite3, path: path, index_append_table_name: true, debug: false)
|
|
41
|
+
BazaModels.primary_db = @db
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def init_database_structure
|
|
45
|
+
@db.tables.create(
|
|
46
|
+
:users,
|
|
47
|
+
columns: [
|
|
48
|
+
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
49
|
+
{name: :organization_id, type: :int},
|
|
50
|
+
{name: :email, type: :varchar},
|
|
51
|
+
{name: :email_confirmation, type: :varchar},
|
|
52
|
+
{name: :created_at, type: :datetime},
|
|
53
|
+
{name: :updated_at, type: :datetime},
|
|
54
|
+
{name: :admin, type: :tinyint}
|
|
55
|
+
],
|
|
56
|
+
indexes: [:organization_id, :email])
|
|
57
|
+
|
|
58
|
+
@db.tables.create(
|
|
59
|
+
:user_passports,
|
|
60
|
+
columns: [
|
|
61
|
+
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
62
|
+
{name: :user_id, type: :int},
|
|
63
|
+
{name: :code, type: :varchar}
|
|
64
|
+
],
|
|
65
|
+
indexes: [
|
|
66
|
+
:user_id
|
|
67
|
+
])
|
|
68
|
+
|
|
69
|
+
@db.tables.create(
|
|
70
|
+
:persons,
|
|
71
|
+
columns: [
|
|
72
|
+
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
73
|
+
{name: :user_id, type: :int}
|
|
74
|
+
],
|
|
75
|
+
indexes: [:user_id])
|
|
76
|
+
|
|
77
|
+
@db.tables.create(
|
|
78
|
+
:roles,
|
|
79
|
+
columns: [
|
|
80
|
+
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
81
|
+
{name: :user_id, type: :int},
|
|
82
|
+
{name: :role, type: :varchar},
|
|
83
|
+
{name: :created_at, type: :datetime},
|
|
84
|
+
{name: :updated_at, type: :datetime}
|
|
85
|
+
],
|
|
86
|
+
indexes: [:user_id]
|
|
87
|
+
)
|
|
88
|
+
|
|
89
|
+
@db.tables.create(
|
|
90
|
+
:organizations,
|
|
91
|
+
columns: [
|
|
92
|
+
{name: :id, type: :int, primarykey: true, autoincr: true},
|
|
93
|
+
{name: :name, type: :varchar}
|
|
94
|
+
]
|
|
95
|
+
)
|
|
96
|
+
end
|
|
90
97
|
end
|
data/spec/test_classes/user.rb
CHANGED
|
@@ -14,6 +14,7 @@ class User < BazaModels::Model
|
|
|
14
14
|
validates_confirmation_of :email, if: :validate_confirmation
|
|
15
15
|
|
|
16
16
|
scope :admin_roles_scope, -> { joins(:roles).where(roles: {role: "administrator"}) }
|
|
17
|
+
scope :created_at_since, ->(date) { where("users.created_at >= ?", date) }
|
|
17
18
|
|
|
18
19
|
before_save do
|
|
19
20
|
self.before_save_block_called ||= 0
|
|
@@ -26,6 +27,10 @@ class User < BazaModels::Model
|
|
|
26
27
|
__send__(callback_type, :add_callback, callback_type)
|
|
27
28
|
end
|
|
28
29
|
|
|
30
|
+
def self.ransackable_scopes(_auth_object = nil)
|
|
31
|
+
%i(created_at_since)
|
|
32
|
+
end
|
|
33
|
+
|
|
29
34
|
private
|
|
30
35
|
|
|
31
36
|
def add_callback(callback_type)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: baza_models
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0.
|
|
4
|
+
version: 0.0.6
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- kaspernj
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-04-
|
|
11
|
+
date: 2016-04-11 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: array_enumerator
|
|
@@ -30,14 +30,14 @@ dependencies:
|
|
|
30
30
|
requirements:
|
|
31
31
|
- - "~>"
|
|
32
32
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: 0.0.
|
|
33
|
+
version: 0.0.21
|
|
34
34
|
type: :runtime
|
|
35
35
|
prerelease: false
|
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
37
|
requirements:
|
|
38
38
|
- - "~>"
|
|
39
39
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: 0.0.
|
|
40
|
+
version: 0.0.21
|
|
41
41
|
- !ruby/object:Gem::Dependency
|
|
42
42
|
name: string-cases
|
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -80,6 +80,20 @@ dependencies:
|
|
|
80
80
|
- - ">="
|
|
81
81
|
- !ruby/object:Gem::Version
|
|
82
82
|
version: 0.0.12
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: simple_delegate
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: 0.0.2
|
|
90
|
+
type: :runtime
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: 0.0.2
|
|
83
97
|
- !ruby/object:Gem::Dependency
|
|
84
98
|
name: rspec
|
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -248,6 +262,7 @@ files:
|
|
|
248
262
|
- lib/baza_models/helpers.rb
|
|
249
263
|
- lib/baza_models/helpers/ransacker_helper.rb
|
|
250
264
|
- lib/baza_models/model.rb
|
|
265
|
+
- lib/baza_models/model/active_record_column_adapater.rb
|
|
251
266
|
- lib/baza_models/model/belongs_to_relations.rb
|
|
252
267
|
- lib/baza_models/model/custom_validations.rb
|
|
253
268
|
- lib/baza_models/model/delegation.rb
|
|
@@ -255,12 +270,14 @@ files:
|
|
|
255
270
|
- lib/baza_models/model/has_one_relations.rb
|
|
256
271
|
- lib/baza_models/model/manipulation.rb
|
|
257
272
|
- lib/baza_models/model/queries.rb
|
|
273
|
+
- lib/baza_models/model/reflection.rb
|
|
258
274
|
- lib/baza_models/model/scopes.rb
|
|
259
275
|
- lib/baza_models/model/translation_functionality.rb
|
|
260
276
|
- lib/baza_models/model/validations.rb
|
|
261
277
|
- lib/baza_models/query.rb
|
|
262
278
|
- lib/baza_models/query/inspector.rb
|
|
263
279
|
- lib/baza_models/query/not.rb
|
|
280
|
+
- lib/baza_models/query/pagination.rb
|
|
264
281
|
- lib/baza_models/query/sql_generator.rb
|
|
265
282
|
- lib/baza_models/ransacker.rb
|
|
266
283
|
- lib/baza_models/ransacker/relationship_scanner.rb
|
|
@@ -289,6 +306,7 @@ files:
|
|
|
289
306
|
- spec/baza_models/model/validations_spec.rb
|
|
290
307
|
- spec/baza_models/model_spec.rb
|
|
291
308
|
- spec/baza_models/query/not_spec.rb
|
|
309
|
+
- spec/baza_models/query/pagination_spec.rb
|
|
292
310
|
- spec/baza_models/query_spec.rb
|
|
293
311
|
- spec/baza_models/ransacker_spec.rb
|
|
294
312
|
- spec/baza_models/validators/confirmation_validator_spec.rb
|