dynamometer 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +84 -16
- data/app/models/concerns/dynamic_attributes.rb +29 -17
- data/dynamometer.gemspec +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2a5a9ab37e0048fa465edda592aa687552fe61b2
|
4
|
+
data.tar.gz: 4b9b62aec8ac9d6d13b2b6df5dc1dff205bd4e96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80be7c4de6046ba5007734148de1ad4568de04125569787cb73ec773cfcdc8d63c8b29c090e6731cacf1bc84c76a26656a8351397a5ee91a7fff04cf76d4b1ad
|
7
|
+
data.tar.gz: 486f7e1f582c0386475d5d621f1c8ecbd66fa9799c5db42166e6db5de1657e69b5e478264dbff2299e6c3d3a35839a346a162c0d5306d3dbee7599f66a91c46e
|
data/README.md
CHANGED
@@ -1,29 +1,97 @@
|
|
1
1
|
# Dynamometer
|
2
2
|
|
3
|
-
|
3
|
+
Adds dynamic attributes to ActiveRecord models
|
4
4
|
|
5
|
-
##
|
5
|
+
## Usage
|
6
6
|
|
7
|
-
|
7
|
+
Generate a migration to enable hstore
|
8
8
|
|
9
|
-
|
9
|
+
class EnableHstore < ActiveRecord::Migration
|
10
|
+
def up
|
11
|
+
enable_extension "hstore"
|
12
|
+
end
|
13
|
+
|
14
|
+
def down
|
15
|
+
disable_extension "hstore"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
Add a dynamic_attributes hstore when creating a table
|
10
20
|
|
11
|
-
|
21
|
+
create_table :users do |t|
|
22
|
+
t.hstore :dynamic_attributes
|
23
|
+
t.index :dynamic_attributes
|
24
|
+
end
|
25
|
+
|
26
|
+
or add dynamic_attributes to an existing table
|
12
27
|
|
13
|
-
|
28
|
+
add_column :users, :dynamic_attributes, :hstore
|
29
|
+
add_index :users, :dynamic_attributes
|
14
30
|
|
15
|
-
|
31
|
+
Add dynamic attributes to your ActiveRecord model
|
16
32
|
|
17
|
-
|
33
|
+
class User < ActiveRecord::Base
|
34
|
+
include DynamicAttributes
|
35
|
+
end
|
18
36
|
|
19
|
-
##
|
37
|
+
## Accessors
|
38
|
+
|
39
|
+
You can read dynamic attributes like you would typical attributes.
|
40
|
+
|
41
|
+
user.category
|
42
|
+
user[:category]
|
43
|
+
user['category']
|
44
|
+
|
45
|
+
You can write dynamic attributes like you would typical attributes, as well.
|
46
|
+
|
47
|
+
user.category = 'superuser'
|
48
|
+
user[:category] = 'superuser'
|
49
|
+
user['category'] = 'superuser'
|
50
|
+
user.update_attribute(:category, 'superuser')
|
51
|
+
user.update_attributes(category: 'superuser')
|
52
|
+
|
53
|
+
Dynamic attributes will appear in your model's attributes `user.attributes` as if they were typical database attributes.
|
54
|
+
|
55
|
+
You can access just the dynamic attributes by calling `dynamic_attributes`.
|
56
|
+
|
57
|
+
## Querying
|
20
58
|
|
21
|
-
|
59
|
+
You can query for matches to dynamic_attributes by calling `where_dynamic_attributes`.
|
22
60
|
|
23
|
-
|
61
|
+
current_site.users.where_dynamic_attributes(category: 'superuser')
|
62
|
+
current_site.users.where_dynamic_attributes(category: 'superuser', name: 'Steve')
|
24
63
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
64
|
+
I can't figure out how to do this correctly in the gem. So, for now to use `where_dynamic_attributes`, put this in your `config/initializers`:
|
65
|
+
|
66
|
+
module ActiveRecord
|
67
|
+
module QueryMethods
|
68
|
+
extend ActiveSupport::Concern
|
69
|
+
|
70
|
+
def where_dynamic_attributes(filters)
|
71
|
+
(filters || {}).each do |k, v|
|
72
|
+
self.where_values += build_where("dynamic_attributes @> hstore(?, ?)", [k, v])
|
73
|
+
end
|
74
|
+
self
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
## ActiveModel Serializers
|
81
|
+
|
82
|
+
If you want to serialize all of your dynamic attributes using activemodel serializers
|
83
|
+
|
84
|
+
class UserSerializer < ActiveModel::Serializer
|
85
|
+
include DynamicAttributesSerializer
|
86
|
+
attributes :id
|
87
|
+
end
|
88
|
+
|
89
|
+
## Installation
|
90
|
+
|
91
|
+
Add this line to your application's Gemfile:
|
92
|
+
|
93
|
+
gem 'dynamometer'
|
94
|
+
|
95
|
+
And then execute:
|
96
|
+
|
97
|
+
$ bundle
|
@@ -2,12 +2,13 @@ module DynamicAttributes
|
|
2
2
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
|
-
def [](
|
6
|
-
|
7
|
-
if attribute_name == 'dynamic_attributes' || !has_dynamic_attribute?(attribute_name)
|
5
|
+
def [](attr_name)
|
6
|
+
if @attributes.keys.include?(attr_name.to_s)
|
8
7
|
super
|
8
|
+
elsif has_dynamic_attribute?(attr_name)
|
9
|
+
read_dynamic_attribute(attr_name)
|
9
10
|
else
|
10
|
-
|
11
|
+
super
|
11
12
|
end
|
12
13
|
end
|
13
14
|
|
@@ -15,17 +16,15 @@ module DynamicAttributes
|
|
15
16
|
begin
|
16
17
|
super
|
17
18
|
rescue ActiveModel::MissingAttributeError
|
18
|
-
|
19
|
+
write_dynamic_attribute(key, value)
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
|
-
def method_missing(
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
args[0]
|
27
|
-
elsif has_dynamic_attribute?(method_name)
|
28
|
-
self[method_name]
|
23
|
+
def method_missing(method, *args)
|
24
|
+
if has_dynamic_attribute?(method)
|
25
|
+
read_dynamic_attribute(method)
|
26
|
+
elsif method =~ /^[\w]+\=$/
|
27
|
+
write_dynamic_attribute(method.to_s.chop, args[0])
|
29
28
|
else
|
30
29
|
super
|
31
30
|
end
|
@@ -33,18 +32,31 @@ module DynamicAttributes
|
|
33
32
|
|
34
33
|
def attributes
|
35
34
|
attrs = super
|
36
|
-
|
37
|
-
|
35
|
+
dynamic_attrs = attrs.delete('dynamic_attributes') || {}
|
36
|
+
dynamic_attrs.merge(attrs)
|
38
37
|
end
|
39
38
|
|
40
39
|
def dynamic_attributes
|
41
|
-
self[
|
40
|
+
self['dynamic_attributes'] || {}
|
41
|
+
end
|
42
|
+
|
43
|
+
def has_attribute?(attr_name)
|
44
|
+
super || has_dynamic_attribute?(attr_name)
|
42
45
|
end
|
43
46
|
|
44
47
|
private
|
45
48
|
|
46
|
-
def has_dynamic_attribute?(
|
47
|
-
dynamic_attributes.has_key?(
|
49
|
+
def has_dynamic_attribute?(attr_name)
|
50
|
+
dynamic_attributes.has_key?(attr_name.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
def read_dynamic_attribute(attr_name)
|
54
|
+
dynamic_attributes[attr_name.to_s]
|
55
|
+
end
|
56
|
+
|
57
|
+
def write_dynamic_attribute(attr_name, value)
|
58
|
+
@attributes.merge('dynamic_attributes' => dynamic_attributes.merge(attr_name.to_s => value))
|
59
|
+
value
|
48
60
|
end
|
49
61
|
|
50
62
|
end
|
data/dynamometer.gemspec
CHANGED
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |spec|
|
6
6
|
spec.name = "dynamometer"
|
7
|
-
spec.version = "0.0.
|
7
|
+
spec.version = "0.0.3"
|
8
8
|
spec.authors = ["John Colvin"]
|
9
9
|
spec.email = ["john.colvin@neo.com"]
|
10
10
|
spec.description = "Adds support for searchable, sortable, dynamic ActiveRecord attributes"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Colvin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|