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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1046c5c0c35f7f1e839dbb022b07674fb2a9a5b7
4
- data.tar.gz: 95bf8c5241b1b9c4aac3aa9067c64dffb1e96122
3
+ metadata.gz: 8381df54a2f01ec5e7a6fc5116c183830a40adc8
4
+ data.tar.gz: 5ca5d4ff2ec9f94eb84cc404b82d29793c3efdf5
5
5
  SHA512:
6
- metadata.gz: ac1fa8bdac3b2314d7eea6e6b9a24e179882f8b5ee74f8be5d3d64be4e922b487a0bfd93e2ce70c07ece18f905688c4cfba6ca7fe525bea9dc7cc42f972db9af
7
- data.tar.gz: 622650be73b4af1e98019e67b7ae80a3ebe93febb407a0f12dc436b5946e34d453dca54b580e76f91eda8e3ba0a2e1c85e79b4c70238bb7935e4e9aa2dc9bde7
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 dynamic_attributes by calling `where_dynamic_attributes`.
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
@@ -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::QueryMethods.module_eval do
12
- def where_dynamic_attributes(filters)
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
@@ -1,3 +1,3 @@
1
1
  module Dynamometer
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -1,6 +1,8 @@
1
1
  class Person < ActiveRecord::Base
2
2
  include DynamicAttributes
3
3
 
4
+ belongs_to :father, class_name: 'Person'
5
+
4
6
  def active_model_serializer
5
7
  PersonSerializer
6
8
  end
@@ -0,0 +1,5 @@
1
+ class AddFatherIdToPeople < ActiveRecord::Migration
2
+ def change
3
+ add_column :people, :father_id, :integer
4
+ end
5
+ end
@@ -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: 20131008210406) do
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.where_dynamic_attributes(magic_level: 'over 9000')
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.where_dynamic_attributes(magic_level: 'over 9000')
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.6
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