redcrumbs 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -137,6 +137,32 @@ end
137
137
 
138
138
  It's not best practice but since the emphasis is on easing the load on our main database we have bent a few rules in order to reduce the calls on the database to, ideally, zero. In any given app you may be tracking several models and this results in a lot of SQL we could do without.
139
139
 
140
+ #### Versions >= 0.3.0
141
+
142
+ `redcrumbed` accepts a `:store` option to which you can pass a hash of options similar to that of the ActiveRecord `as_json` method. These are attributes of the subject that you'd like to store on the crumb object itself. Use it sparingly if you know that, for example, you are only ever going to really use a couple of attributes of the subject and you want to avoid loading the whole thing from the database.
143
+
144
+ Examples:
145
+
146
+ ```
147
+ class Venue
148
+ redcrumbed :only => [:name, :latlng], :store => {:only => [:id, :name]}
149
+ end
150
+ ```
151
+
152
+ ```
153
+ class Venue
154
+ redcrumbed :only => [:name, :latlng], :store => {:except => [:updated_at, :created_at]}
155
+ end
156
+ ```
157
+
158
+ ```
159
+ class Venue
160
+ redcrumbed :only => [:name, :latlng], :store => {:only => [:id, :name], :methods => [:checkins]}
161
+ end
162
+ ```
163
+
164
+ #### Versions < 0.3.0
165
+
140
166
  `redcrumbed` accepts a `:store` option to which you can pass an array of attributes of the subject that you'd like to store on the crumb object itself. Use it sparingly if you know that, for example, you are only ever going to really use a couple of attributes of the subject and you want to avoid loading the whole thing from the database.
141
167
 
142
168
  ```
@@ -145,7 +171,13 @@ class Venue
145
171
  end
146
172
  ```
147
173
 
148
- So now if you call `crumb.subject` instead of loading the Venue from your database it will instantiate a new Venue with the same `id` and `name` attributes. You can always retrieve the original by calling `crumb.full_subject`.
174
+ #### Using the stored object
175
+
176
+ So now if you call `crumb.subject` instead of loading the Venue from your database it will instantiate a new Venue with the only the attributes you have stored. You can always retrieve the original by calling `crumb.full_subject`.
177
+
178
+ _ If you plan to use the `methods` option to store data on the Crumb you should only use it to store attr_accessors unless you won't be instantiating the subject itself _
179
+
180
+ #### Creator and Target storage
149
181
 
150
182
  As you might expect, you can also do this for the creator and target of the crumb. See the redcrumbs.rb initializer for how to set this as a global configuration.
151
183
 
@@ -4,7 +4,7 @@ module Redcrumbs
4
4
 
5
5
  def subject=(subject)
6
6
  self._subject = subject
7
- self.stored_subject = subject.storeable_attributes
7
+ self.stored_subject = subject.storeable_attributes_and_method_attributes
8
8
  self.subject_type = subject.class.to_s
9
9
  self.subject_id = subject.id
10
10
  end
@@ -12,7 +12,30 @@ module Redcrumbs
12
12
  end
13
13
 
14
14
  def storeable_attributes
15
- attributes.reject {|k,v| !self.class.redcrumbs_options[:store].include?(k.to_sym)}
15
+ store = self.class.redcrumbs_options[:store]
16
+ if store.has_key?(:only)
17
+ attributes.reject {|k,v| !store[:only].include?(k.to_sym)}
18
+ elsif store.has_key?(:except)
19
+ attributes.reject {|k,v| store[:except].include?(k.to_sym)}
20
+ else
21
+ {}
22
+ end
23
+ end
24
+
25
+ def attributes_from_storeable_methods
26
+ store = self.class.redcrumbs_options[:store]
27
+ if store.has_key?(:methods)
28
+ # get the methods that actually exist on the model
29
+ methods = methods_from_array(store[:methods])
30
+ # inject them into a hash with their outcomes as values
31
+ methods.inject({}) {|h,a| h.merge(a => send(a))}
32
+ else
33
+ {}
34
+ end
35
+ end
36
+
37
+ def storeable_attributes_and_method_attributes
38
+ storeable_attributes.merge(attributes_from_storeable_methods)
16
39
  end
17
40
 
18
41
  def create_crumb
@@ -24,6 +47,12 @@ module Redcrumbs
24
47
  def notify_changes
25
48
  create_crumb unless watched_changes.empty?
26
49
  end
50
+
51
+ private
52
+
53
+ def methods_from_array(array)
54
+ self.class.instance_methods.select {|method| array.include?(method.to_sym)}
55
+ end
27
56
  end
28
57
  end
29
58
  end
@@ -14,7 +14,7 @@ module Redcrumbs
14
14
  options.reverse_merge!(defaults)
15
15
 
16
16
  options[:only] = Array(options[:only])
17
- options[:store] = Array(options[:store])
17
+ options[:store] = options[:store]
18
18
 
19
19
  class_attribute :redcrumbs_options
20
20
  class_attribute :redcrumbs_callback_options
@@ -1,3 +1,3 @@
1
1
  module Redcrumbs
2
- VERSION = "0.2.5"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: redcrumbs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-07 00:00:00.000000000 +01:00
12
+ date: 2012-09-11 00:00:00.000000000 +01:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: data_mapper
17
- requirement: &2156957200 !ruby/object:Gem::Requirement
17
+ requirement: &2152134000 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.2.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2156957200
25
+ version_requirements: *2152134000
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: dm-redis-adapter
28
- requirement: &2156956660 !ruby/object:Gem::Requirement
28
+ requirement: &2152133500 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: 0.6.2
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2156956660
36
+ version_requirements: *2152133500
37
37
  description: Fast and unobtrusive activity tracking of ActiveRecord models using DataMapper
38
38
  and Redis
39
39
  email: