rails_agnostic_models 0.0.7 → 0.0.8
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 +15 -0
- data/lib/rails_agnostic_models/active_record_extensions.rb +3 -95
- data/lib/rails_agnostic_models/arel_translator/collection_converters.rb +26 -0
- data/lib/rails_agnostic_models/arel_translator/order.rb +15 -0
- data/lib/rails_agnostic_models/arel_translator/translator.rb +47 -0
- data/lib/rails_agnostic_models/arel_translator/where.rb +26 -0
- data/lib/rails_agnostic_models/arel_translator.rb +4 -0
- data/lib/rails_agnostic_models/rails_helpers.rb +52 -0
- data/lib/rails_agnostic_models/version.rb +1 -1
- data/lib/rails_agnostic_models.rb +2 -0
- data/spec/rails_agnostic_models/version_agnostic_default_scope_spec.rb +2 -2
- metadata +11 -27
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZWI1MTNmNjU2ZDkzYjlmOThiNWZlZDcxYmFjNWJiODQyYTkyMWJiYQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MzRkZmQ0YTQ3MTNiNmQwNjAzOGY1YzE2ZjRmNWUzNWUxOTBmMWJlMg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NjBlYmY2Njc0MDNlMjJlMzJmNjI1Y2VkMDdlYWRiM2NiNWJlNzM0NjQyMzM2
|
10
|
+
Y2FkZTljMGM5NWU2Y2ExNjk0ZDNlZWFjMDRhZGU1MzNmZGY1MGVmZmEwZTZm
|
11
|
+
OTI0OTFlYzVhMDY0ODkzNDU1NDk0ODhlYzBkM2E0NWQ2ODliMTI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YTE0N2QwMTYxYjlmMjUwNzQyYWMxN2IzZTFmZjNiZTkyNTU2NmE1NjYzN2Jj
|
14
|
+
ZjliOWU4ZThjZjUyYTZhZmU4NjM2YzdlMzc0MzY0Y2UzOGQwMzFkMGExZjQ2
|
15
|
+
YTQ5YzU3ZTQ0YjIzYWM5OGYzYTBiYzI1YjcyMmNlYzFlOGU1Yzg=
|
@@ -1,52 +1,8 @@
|
|
1
|
+
require_relative './rails_helpers'
|
1
2
|
module RailsAgnosticModels
|
2
3
|
module ActiveRecordExtensions
|
3
4
|
module ClassMethods
|
4
5
|
require 'ostruct'
|
5
|
-
|
6
|
-
# Checks if the host app is a Rails 2 app
|
7
|
-
def rails_2?
|
8
|
-
rails_loaded && (Rails::VERSION::MAJOR == 2)
|
9
|
-
end
|
10
|
-
|
11
|
-
# Checks if the host app is a Rails 3 app
|
12
|
-
def rails_3?
|
13
|
-
rails_loaded && (Rails::VERSION::MAJOR == 3)
|
14
|
-
end
|
15
|
-
|
16
|
-
# Checks if the host app is a Rails 4 app
|
17
|
-
def rails_4?
|
18
|
-
rails_loaded && (Rails::VERSION::MAJOR == 4)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Checks if Rails is loaded
|
22
|
-
def rails_loaded
|
23
|
-
defined? Rails
|
24
|
-
end
|
25
|
-
|
26
|
-
# Takes a block that will only be executed in a Rails 2 Project
|
27
|
-
def rails_2(&block)
|
28
|
-
return nil unless rails_2?
|
29
|
-
yield
|
30
|
-
end
|
31
|
-
|
32
|
-
# Code to be executed insode of a Rails 3 app
|
33
|
-
def rails_3(&block)
|
34
|
-
return nil unless rails_3?
|
35
|
-
yield
|
36
|
-
end
|
37
|
-
|
38
|
-
# Code to be executed inside of a Rails 4 app
|
39
|
-
def rails_4(&block)
|
40
|
-
return nil unless rails_4?
|
41
|
-
yield
|
42
|
-
end
|
43
|
-
|
44
|
-
# Code to be executed only inside of a rails app
|
45
|
-
def rails(&block)
|
46
|
-
return nil unless rails_loaded
|
47
|
-
yield
|
48
|
-
end
|
49
|
-
|
50
6
|
# safely refer to constants that may not be defined. Useful in a gem that might get included in places that might not define EVERY active record model, such as
|
51
7
|
# a satelite administration application. Note that you will still need to handle nil cases
|
52
8
|
#
|
@@ -88,7 +44,7 @@ module RailsAgnosticModels
|
|
88
44
|
if rails_2?
|
89
45
|
default_scope options
|
90
46
|
else
|
91
|
-
self.instance_eval "default_scope #{
|
47
|
+
self.instance_eval "default_scope #{ArelTranslator::Translator.new(options).translate!}"
|
92
48
|
end
|
93
49
|
end
|
94
50
|
|
@@ -113,58 +69,10 @@ module RailsAgnosticModels
|
|
113
69
|
def top_level_constant_lookup(constant)
|
114
70
|
Object.const_get(constant) if Object.const_defined? constant
|
115
71
|
end
|
116
|
-
|
117
|
-
def options_to_arel(options)
|
118
|
-
first_key = options.keys.first
|
119
|
-
option = OpenStruct.new
|
120
|
-
option.key = first_key
|
121
|
-
option.value = options[first_key]
|
122
|
-
code = "#{translate_arel(option)}"
|
123
|
-
options.keys.drop(1).each do |opt|
|
124
|
-
option.key = opt
|
125
|
-
option.value = options[opt]
|
126
|
-
code += ".#{translate_arel(option)}"
|
127
|
-
end
|
128
|
-
code
|
129
|
-
end
|
130
|
-
|
131
|
-
def translate_arel(option)
|
132
|
-
case option.key
|
133
|
-
when :order then "order('#{option.value}')"
|
134
|
-
when :conditions then "where(#{translate_where(option.value)})"
|
135
|
-
else ""
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
def translate_where(conditions)
|
140
|
-
case conditions
|
141
|
-
when Hash then return hash_without_braches(conditions)
|
142
|
-
when String then return "\"#{conditions}\""
|
143
|
-
when Array then return array_without_brackets(conditions)
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
def hash_without_braches(hash)
|
148
|
-
return hash.keys.inject([]) do |a, key|
|
149
|
-
a << "#{key}: #{hash[key]}"
|
150
|
-
end.join(', ')
|
151
|
-
end
|
152
|
-
|
153
|
-
def array_without_brackets(array)
|
154
|
-
return array.inject([]) do |a, value|
|
155
|
-
a << array_value(value)
|
156
|
-
end.join(", ")
|
157
|
-
end
|
158
|
-
|
159
|
-
def array_value(value)
|
160
|
-
case value
|
161
|
-
when String then return "\"#{value}\""
|
162
|
-
else return value
|
163
|
-
end
|
164
|
-
end
|
165
72
|
end
|
166
73
|
def self.included(klass)
|
167
74
|
klass.extend(ClassMethods)
|
75
|
+
klass.extend(RailsAgnosticModels::RailsHelpers::ClassMethods)
|
168
76
|
end
|
169
77
|
end
|
170
78
|
ActiveRecord::Base.send(:include, ActiveRecordExtensions)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RailsAgnosticModels
|
2
|
+
module ArelTranslator
|
3
|
+
module CollectionConverters
|
4
|
+
private
|
5
|
+
|
6
|
+
def hash_without_braches(hash)
|
7
|
+
return hash.keys.inject([]) do |a, key|
|
8
|
+
a << "#{key}: #{hash[key]}"
|
9
|
+
end.join(', ')
|
10
|
+
end
|
11
|
+
|
12
|
+
def array_without_brackets(array)
|
13
|
+
return array.inject([]) do |a, value|
|
14
|
+
a << array_value(value)
|
15
|
+
end.join(", ")
|
16
|
+
end
|
17
|
+
|
18
|
+
def array_value(value)
|
19
|
+
case value
|
20
|
+
when String then return "\"#{value}\""
|
21
|
+
else return value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module RailsAgnosticModels
|
2
|
+
module ArelTranslator
|
3
|
+
class Translator
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
attr_accessor :search_hash, :arel_string
|
7
|
+
|
8
|
+
def initialize(search_hash)
|
9
|
+
@search_hash = search_hash
|
10
|
+
@arel_string = ""
|
11
|
+
end
|
12
|
+
|
13
|
+
def translate!
|
14
|
+
options_to_arel(search_hash)
|
15
|
+
arel_string
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def options_to_arel(options)
|
21
|
+
first_key = options.keys.first
|
22
|
+
option = hash_to_ostruct(first_key, options[first_key])
|
23
|
+
self.arel_string << "#{translate_arel(option)}"
|
24
|
+
options.keys.drop(1).each do |opt|
|
25
|
+
option = hash_to_ostruct(opt, options[opt])
|
26
|
+
self.arel_string << ".#{translate_arel(option)}"
|
27
|
+
end
|
28
|
+
self.arel_string
|
29
|
+
end
|
30
|
+
|
31
|
+
def hash_to_ostruct(key, value)
|
32
|
+
option = OpenStruct.new
|
33
|
+
option.key = key
|
34
|
+
option.value = value
|
35
|
+
option
|
36
|
+
end
|
37
|
+
|
38
|
+
def translate_arel(option)
|
39
|
+
case option.key
|
40
|
+
when :order then Order.new(option.value)
|
41
|
+
when :conditions then Where.new(option.value)
|
42
|
+
else ""
|
43
|
+
end.translate!
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module RailsAgnosticModels
|
2
|
+
module ArelTranslator
|
3
|
+
class Where
|
4
|
+
include CollectionConverters
|
5
|
+
attr_accessor :conditions
|
6
|
+
|
7
|
+
def initialize(conditions)
|
8
|
+
@conditions = conditions
|
9
|
+
end
|
10
|
+
|
11
|
+
def translate!
|
12
|
+
case self.conditions
|
13
|
+
when Hash then return wrap_where hash_without_braches(self.conditions)
|
14
|
+
when String then return wrap_where "\"#{self.conditions}\""
|
15
|
+
when Array then return wrap_where array_without_brackets(self.conditions)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def wrap_where(str)
|
22
|
+
"where(#{str})"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module RailsAgnosticModels
|
2
|
+
module RailsHelpers
|
3
|
+
module ClassMethods
|
4
|
+
# Checks if the host app is a Rails 2 app
|
5
|
+
def rails_2?
|
6
|
+
rails_loaded && (Rails::VERSION::MAJOR == 2)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Checks if the host app is a Rails 3 app
|
10
|
+
def rails_3?
|
11
|
+
rails_loaded && (Rails::VERSION::MAJOR == 3)
|
12
|
+
end
|
13
|
+
|
14
|
+
# Checks if the host app is a Rails 4 app
|
15
|
+
def rails_4?
|
16
|
+
rails_loaded && (Rails::VERSION::MAJOR == 4)
|
17
|
+
end
|
18
|
+
|
19
|
+
# Checks if Rails is loaded
|
20
|
+
def rails_loaded
|
21
|
+
defined? Rails
|
22
|
+
end
|
23
|
+
|
24
|
+
# Takes a block that will only be executed in a Rails 2 Project
|
25
|
+
def rails_2(&block)
|
26
|
+
return nil unless rails_2?
|
27
|
+
yield
|
28
|
+
end
|
29
|
+
|
30
|
+
# Code to be executed insode of a Rails 3 app
|
31
|
+
def rails_3(&block)
|
32
|
+
return nil unless rails_3?
|
33
|
+
yield
|
34
|
+
end
|
35
|
+
|
36
|
+
# Code to be executed inside of a Rails 4 app
|
37
|
+
def rails_4(&block)
|
38
|
+
return nil unless rails_4?
|
39
|
+
yield
|
40
|
+
end
|
41
|
+
|
42
|
+
# Code to be executed only inside of a rails app
|
43
|
+
def rails(&block)
|
44
|
+
return nil unless rails_loaded
|
45
|
+
yield
|
46
|
+
end
|
47
|
+
end
|
48
|
+
def self.included(klass)
|
49
|
+
klass.extend(ClassMethods)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -16,9 +16,9 @@ describe "#version_agnostic_default_scope" do
|
|
16
16
|
end
|
17
17
|
context "Rails 3" do
|
18
18
|
before { stub_const("Rails::VERSION::MAJOR", 3) }
|
19
|
-
describe "order" do
|
19
|
+
describe "order", focus: true do
|
20
20
|
let(:hash) { {order: "my_column desc"} }
|
21
|
-
let(:arel) { "default_scope order(
|
21
|
+
let(:arel) { "default_scope order(\"#{hash[:order]}\")" }
|
22
22
|
it "converts options hash to arel calls" do
|
23
23
|
Rails3Class.should_receive(:instance_eval).with(arel)
|
24
24
|
Rails3Class.send(:version_agnostic_default_scope, hash)
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_agnostic_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.8
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- DVG
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date: 2014-03-
|
11
|
+
date: 2014-03-18 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activerecord
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ! '>='
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ! '>='
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: bundler
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :development
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -46,7 +41,6 @@ dependencies:
|
|
46
41
|
- !ruby/object:Gem::Dependency
|
47
42
|
name: rake
|
48
43
|
requirement: !ruby/object:Gem::Requirement
|
49
|
-
none: false
|
50
44
|
requirements:
|
51
45
|
- - ! '>='
|
52
46
|
- !ruby/object:Gem::Version
|
@@ -54,7 +48,6 @@ dependencies:
|
|
54
48
|
type: :development
|
55
49
|
prerelease: false
|
56
50
|
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
none: false
|
58
51
|
requirements:
|
59
52
|
- - ! '>='
|
60
53
|
- !ruby/object:Gem::Version
|
@@ -62,7 +55,6 @@ dependencies:
|
|
62
55
|
- !ruby/object:Gem::Dependency
|
63
56
|
name: sqlite3
|
64
57
|
requirement: !ruby/object:Gem::Requirement
|
65
|
-
none: false
|
66
58
|
requirements:
|
67
59
|
- - ! '>='
|
68
60
|
- !ruby/object:Gem::Version
|
@@ -70,7 +62,6 @@ dependencies:
|
|
70
62
|
type: :development
|
71
63
|
prerelease: false
|
72
64
|
version_requirements: !ruby/object:Gem::Requirement
|
73
|
-
none: false
|
74
65
|
requirements:
|
75
66
|
- - ! '>='
|
76
67
|
- !ruby/object:Gem::Version
|
@@ -78,7 +69,6 @@ dependencies:
|
|
78
69
|
- !ruby/object:Gem::Dependency
|
79
70
|
name: rspec
|
80
71
|
requirement: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
72
|
requirements:
|
83
73
|
- - ! '>='
|
84
74
|
- !ruby/object:Gem::Version
|
@@ -86,7 +76,6 @@ dependencies:
|
|
86
76
|
type: :development
|
87
77
|
prerelease: false
|
88
78
|
version_requirements: !ruby/object:Gem::Requirement
|
89
|
-
none: false
|
90
79
|
requirements:
|
91
80
|
- - ! '>='
|
92
81
|
- !ruby/object:Gem::Version
|
@@ -94,7 +83,6 @@ dependencies:
|
|
94
83
|
- !ruby/object:Gem::Dependency
|
95
84
|
name: database_cleaner
|
96
85
|
requirement: !ruby/object:Gem::Requirement
|
97
|
-
none: false
|
98
86
|
requirements:
|
99
87
|
- - ! '>='
|
100
88
|
- !ruby/object:Gem::Version
|
@@ -102,7 +90,6 @@ dependencies:
|
|
102
90
|
type: :development
|
103
91
|
prerelease: false
|
104
92
|
version_requirements: !ruby/object:Gem::Requirement
|
105
|
-
none: false
|
106
93
|
requirements:
|
107
94
|
- - ! '>='
|
108
95
|
- !ruby/object:Gem::Version
|
@@ -110,7 +97,6 @@ dependencies:
|
|
110
97
|
- !ruby/object:Gem::Dependency
|
111
98
|
name: debugger
|
112
99
|
requirement: !ruby/object:Gem::Requirement
|
113
|
-
none: false
|
114
100
|
requirements:
|
115
101
|
- - ! '>='
|
116
102
|
- !ruby/object:Gem::Version
|
@@ -118,7 +104,6 @@ dependencies:
|
|
118
104
|
type: :development
|
119
105
|
prerelease: false
|
120
106
|
version_requirements: !ruby/object:Gem::Requirement
|
121
|
-
none: false
|
122
107
|
requirements:
|
123
108
|
- - ! '>='
|
124
109
|
- !ruby/object:Gem::Version
|
@@ -139,6 +124,12 @@ files:
|
|
139
124
|
- acts_as_taggable_on.sqlite3
|
140
125
|
- lib/rails_agnostic_models.rb
|
141
126
|
- lib/rails_agnostic_models/active_record_extensions.rb
|
127
|
+
- lib/rails_agnostic_models/arel_translator.rb
|
128
|
+
- lib/rails_agnostic_models/arel_translator/collection_converters.rb
|
129
|
+
- lib/rails_agnostic_models/arel_translator/order.rb
|
130
|
+
- lib/rails_agnostic_models/arel_translator/translator.rb
|
131
|
+
- lib/rails_agnostic_models/arel_translator/where.rb
|
132
|
+
- lib/rails_agnostic_models/rails_helpers.rb
|
142
133
|
- lib/rails_agnostic_models/version.rb
|
143
134
|
- rails_agnostic_models.gemspec
|
144
135
|
- spec/rails_agnostic_models/safe_constant_spec.rb
|
@@ -149,33 +140,26 @@ files:
|
|
149
140
|
homepage: ''
|
150
141
|
licenses:
|
151
142
|
- MIT
|
143
|
+
metadata: {}
|
152
144
|
post_install_message:
|
153
145
|
rdoc_options: []
|
154
146
|
require_paths:
|
155
147
|
- lib
|
156
148
|
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
-
none: false
|
158
149
|
requirements:
|
159
150
|
- - ! '>='
|
160
151
|
- !ruby/object:Gem::Version
|
161
152
|
version: '0'
|
162
|
-
segments:
|
163
|
-
- 0
|
164
|
-
hash: -1728372191715978669
|
165
153
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
166
|
-
none: false
|
167
154
|
requirements:
|
168
155
|
- - ! '>='
|
169
156
|
- !ruby/object:Gem::Version
|
170
157
|
version: '0'
|
171
|
-
segments:
|
172
|
-
- 0
|
173
|
-
hash: -1728372191715978669
|
174
158
|
requirements: []
|
175
159
|
rubyforge_project:
|
176
|
-
rubygems_version:
|
160
|
+
rubygems_version: 2.2.2
|
177
161
|
signing_key:
|
178
|
-
specification_version:
|
162
|
+
specification_version: 4
|
179
163
|
summary: Extends activerecord to provide rails-agnostic versions of common model code
|
180
164
|
to east the pain of upgrading
|
181
165
|
test_files:
|