dynamometer 0.0.6 → 0.0.7
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/README.md +8 -2
- data/app/models/concerns/dynamic_attributes.rb +8 -0
- data/lib/dynamometer/dynamic_attributes_in_where.rb +22 -0
- data/lib/dynamometer/railtie.rb +6 -8
- data/lib/dynamometer/version.rb +1 -1
- data/test/dummy/app/models/person.rb +2 -0
- data/test/dummy/db/migrate/20131011183117_add_father_id_to_people.rb +5 -0
- data/test/dummy/db/schema.rb +2 -1
- data/test/person_test.rb +22 -2
- metadata +4 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8381df54a2f01ec5e7a6fc5116c183830a40adc8
|
4
|
+
data.tar.gz: 5ca5d4ff2ec9f94eb84cc404b82d29793c3efdf5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a2bb08080e830c9cf61b00b774816195741e31be5393619bf2af076118fbb71e3504b17b381ee23589ee39ceb58c0a3c836ce0c701b54c5cb6d0ae02dcde6d27
|
7
|
+
data.tar.gz: 3d8fae53ed967b10910ad15cfe0a8ea70e4095a3c41a5d8e742c789fecd0f2270e36e6e3e9759e46d5959112a94404216b1bfbc420095ff2317697841faddf75
|
data/README.md
CHANGED
@@ -56,10 +56,16 @@ You can access just the dynamic attributes by calling `dynamic_attributes`.
|
|
56
56
|
|
57
57
|
## Querying
|
58
58
|
|
59
|
-
You can query for matches to
|
59
|
+
You can query for matches to dynamic attributes just like regular attributes.
|
60
|
+
|
61
|
+
current_site.users.where(category: 'superuser')
|
62
|
+
current_site.users.where(category: 'superuser', name: 'Steve')
|
63
|
+
|
64
|
+
You can also query for matches to dynamic attributes by calling
|
65
|
+
`where_dynamic_attributes`. Unlike `where` above, this will not work if you
|
66
|
+
mix dynamic and regular attributes.
|
60
67
|
|
61
68
|
current_site.users.where_dynamic_attributes(category: 'superuser')
|
62
|
-
current_site.users.where_dynamic_attributes(category: 'superuser', name: 'Steve')
|
63
69
|
|
64
70
|
## ActiveModel Serializers
|
65
71
|
|
@@ -2,6 +2,14 @@ module DynamicAttributes
|
|
2
2
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
+
module ClassMethods
|
6
|
+
# we only define this for classes with dynamic attributes
|
7
|
+
# so that unmodified classes work exactly as before
|
8
|
+
def partition_wheres(wheres)
|
9
|
+
wheres.partition { |k, v| column_names.include?(k.to_s) || reflect_on_association(k.to_sym) }.map { |x| Hash[x] }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
5
13
|
def [](attr_name)
|
6
14
|
if @attributes.has_key?(attr_name.to_s)
|
7
15
|
super
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Dynamometer
|
2
|
+
module DynamicAttributesInWhere
|
3
|
+
|
4
|
+
def where(*args)
|
5
|
+
return super if !args.first.is_a?(Hash) || args.first.keys.any? { |x| x.is_a?(Hash) }
|
6
|
+
regulars, dynamics = klass.partition_wheres(args.first)
|
7
|
+
if dynamics.empty?
|
8
|
+
super
|
9
|
+
else
|
10
|
+
super(regulars).where_dynamic_attributes(dynamics)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def where_dynamic_attributes(filters)
|
15
|
+
spawn.tap do |new_rel|
|
16
|
+
(filters || {}).each do |k, v|
|
17
|
+
new_rel.where_values += build_where("dynamic_attributes @> hstore(?, ?)", [k, v])
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
data/lib/dynamometer/railtie.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'dynamometer/dynamic_attributes_in_where'
|
2
|
+
|
1
3
|
module Dynamometer
|
2
4
|
class Railtie < Rails::Engine
|
3
5
|
railtie_name :dynamometer
|
@@ -8,15 +10,11 @@ module Dynamometer
|
|
8
10
|
delegate :where_dynamic_attributes, to: :all
|
9
11
|
end
|
10
12
|
|
11
|
-
ActiveRecord::
|
12
|
-
def
|
13
|
-
spawn.tap do |new_rel|
|
14
|
-
(filters || {}).each do |k, v|
|
15
|
-
new_rel.where_values += build_where("dynamic_attributes @> hstore(?, ?)", [k, v])
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
13
|
+
ActiveRecord::Base.class_eval do
|
14
|
+
def self.partition_wheres(args); [args,{}]; end
|
19
15
|
end
|
16
|
+
|
17
|
+
ActiveRecord::Relation.send(:include, DynamicAttributesInWhere)
|
20
18
|
end
|
21
19
|
end
|
22
20
|
end
|
data/lib/dynamometer/version.rb
CHANGED
data/test/dummy/db/schema.rb
CHANGED
@@ -11,12 +11,13 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20131011183117) do
|
15
15
|
|
16
16
|
create_table "people", force: true do |t|
|
17
17
|
t.string "name", null: false
|
18
18
|
t.integer "age"
|
19
19
|
t.hstore "dynamic_attributes"
|
20
|
+
t.integer "father_id"
|
20
21
|
end
|
21
22
|
|
22
23
|
add_index "people", ["dynamic_attributes"], name: "index_people_on_dynamic_attributes", using: :btree
|
data/test/person_test.rb
CHANGED
@@ -78,14 +78,34 @@ class PersonTest < ActiveSupport::TestCase
|
|
78
78
|
|
79
79
|
test "can find records by a dynamic attribute" do
|
80
80
|
@person = Person.create(name: 'Nobody', magic_level: 'over 9000')
|
81
|
-
results = Person.
|
81
|
+
results = Person.where(magic_level: 'over 9000')
|
82
|
+
assert results.present?
|
83
|
+
assert_equal @person, results.first
|
84
|
+
end
|
85
|
+
|
86
|
+
test "can find records by a dynamic attribute and a static one" do
|
87
|
+
@person = Person.create(name: 'Nobody', magic_level: 'over 9000')
|
88
|
+
results = Person.where(name: 'Nobody', magic_level: 'over 9000')
|
89
|
+
assert results.present?
|
90
|
+
assert_equal @person, results.first
|
91
|
+
end
|
92
|
+
|
93
|
+
test "can find records by a nonexistent dynamic attribute" do
|
94
|
+
results = Person.where(attribute_not_appearing_in_this_gem: 'wat')
|
95
|
+
assert results.empty?
|
96
|
+
end
|
97
|
+
|
98
|
+
test "can find records via associations" do
|
99
|
+
father = Person.create(name: 'Somebody')
|
100
|
+
@person = Person.create(name: 'Nobody', magic_level: 'over 9000', father: father)
|
101
|
+
results = Person.where(magic_level: 'over 9000', father: father)
|
82
102
|
assert results.present?
|
83
103
|
assert_equal @person, results.first
|
84
104
|
end
|
85
105
|
|
86
106
|
test "chaining does not damage original Relation" do
|
87
107
|
original_relation = Person.where(id: 1)
|
88
|
-
new_relation = original_relation.
|
108
|
+
new_relation = original_relation.where(magic_level: 'over 9000')
|
89
109
|
duplicate_relation = Person.where(id: 1)
|
90
110
|
assert_equal duplicate_relation.where_values, original_relation.where_values
|
91
111
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamometer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Colvin
|
@@ -68,6 +68,7 @@ files:
|
|
68
68
|
- app/serializers/dynamic_attributes_serializer.rb
|
69
69
|
- dynamometer.gemspec
|
70
70
|
- lib/dynamometer.rb
|
71
|
+
- lib/dynamometer/dynamic_attributes_in_where.rb
|
71
72
|
- lib/dynamometer/railtie.rb
|
72
73
|
- lib/dynamometer/version.rb
|
73
74
|
- lib/tasks/dynamometer_tasks.rake
|
@@ -107,6 +108,7 @@ files:
|
|
107
108
|
- test/dummy/config/routes.rb
|
108
109
|
- test/dummy/db/migrate/20131008210130_add_hstore.rb
|
109
110
|
- test/dummy/db/migrate/20131008210406_create_model.rb
|
111
|
+
- test/dummy/db/migrate/20131011183117_add_father_id_to_people.rb
|
110
112
|
- test/dummy/db/schema.rb
|
111
113
|
- test/dummy/lib/assets/.keep
|
112
114
|
- test/dummy/log/.keep
|
@@ -179,6 +181,7 @@ test_files:
|
|
179
181
|
- test/dummy/config/routes.rb
|
180
182
|
- test/dummy/db/migrate/20131008210130_add_hstore.rb
|
181
183
|
- test/dummy/db/migrate/20131008210406_create_model.rb
|
184
|
+
- test/dummy/db/migrate/20131011183117_add_father_id_to_people.rb
|
182
185
|
- test/dummy/db/schema.rb
|
183
186
|
- test/dummy/lib/assets/.keep
|
184
187
|
- test/dummy/log/.keep
|