power_enum 0.5.3 → 0.5.4

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.
data/README.md CHANGED
@@ -324,12 +324,13 @@ the `has_enumerated` macro behaves more like an aggregation than an association.
324
324
  :foreign_key => 'status_id',
325
325
  :on_lookup_failure => :optional_instance_method,
326
326
  :permit_empty_name => true, #Setting this to true disables automatic conversion of empty strings to nil. Default is false.
327
- :default => :unconfirmed #Default value of the attribute.
327
+ :default => :unconfirmed, #Default value of the attribute.
328
+ :create_scope => false #Setting this to false disables the automatic creation of the 'with_status' scope.
328
329
  end
329
330
 
330
331
  By default, the foreign key is interpreted to be the name of your has\_enumerated field (in this case 'booking_status') plus '\_id'. Since we
331
332
  chose to make the column name 'status\_id' for the sake of brevity, we must explicitly designate it. Additionally, the default value for
332
- `:class_name` is the camel-ized version of the name for your has\_enumerated field. `:on_lookup_failure` is explained below. `:permit_empty_name`
333
+ `:class_name` is the camelized version of the name for your has\_enumerated field. `:on_lookup_failure` is explained below. `:permit_empty_name`
333
334
  is an optional flag to disable automatic conversion of empty strings to nil. It is typically desirable to have `booking.update_attributes(:status => '')`
334
335
  assign status_id to a nil rather than raise an Error, as you'll be often calling `update_attributes` with form data, but
335
336
  the choice is yours. Setting a `:default` option will generate an after_initialize callback to set a default value on the
@@ -374,9 +375,26 @@ for all has\_enumerated fields if you happen to have more than one defined in yo
374
375
  NOTE: A `nil` is always considered to be a valid value for `status=(arg)` since it's assumed you're trying to null out the foreign key.
375
376
  The `:on_lookup_failure` will be bypassed.
376
377
 
378
+ #### with\_enumerated\_attribute scope
379
+
380
+ Unless the `:create\_scope` option is set to `false`, a scope is automatically created that takes a list of enums as arguments. This
381
+ allows us to say things like:
382
+
383
+ Booking.with_status :confirmed, :received
384
+
385
+ Strings, symbols, ids, or enum instances are all valid arguments. For example, the following would be valid, though not recommended
386
+ for obvious reasons.
387
+
388
+ Booking.with_status 1, 'confirmed', BookingStatus[:rejected]
389
+
390
+ ### ActiveRecord::Base Extensions
391
+
392
+ The following methods are added to ActiveRecord::Base as class methods.
393
+
377
394
  #### has\_enumerated?(attr)
378
395
 
379
- Returns true if the given attr is an enumerated attributes, false otherwise. `attr` can be a string or a symbol.
396
+ Returns true if the given attr is an enumerated attributes, false otherwise. `attr` can be a string or a symbol. This
397
+ is a class method.
380
398
 
381
399
  #### enumerated\_attributes
382
400
 
@@ -46,6 +46,8 @@ module ActiveRecord
46
46
  # [:default]
47
47
  # Setting this option will generate an after_initialize callback to set a default value on the attribute
48
48
  # unless a non-nil one already exists.
49
+ # [:create_scope]
50
+ # Setting this option to 'false' will disable automatically creating a 'with_enum_attribute' scope.
49
51
  #
50
52
  # === Example
51
53
  # class Booking < ActiveRecord::Base
@@ -53,22 +55,29 @@ module ActiveRecord
53
55
  # :class_name => 'BookingStatus',
54
56
  # :foreign_key => 'status_id',
55
57
  # :on_lookup_failure => :optional_instance_method,
56
- # :permit_empty_name => true
57
- # :default => :unconfirmed
58
+ # :permit_empty_name => true,
59
+ # :default => :unconfirmed,
60
+ # :create_cope => false
58
61
  # end
59
62
  def has_enumerated(part_id, options = {})
60
- options.assert_valid_keys(:class_name, :foreign_key, :on_lookup_failure, :permit_empty_name, :default)
63
+ options.assert_valid_keys( :class_name,
64
+ :foreign_key,
65
+ :on_lookup_failure,
66
+ :permit_empty_name,
67
+ :default,
68
+ :create_scope )
61
69
 
62
70
  reflection = PowerEnum::Reflection::EnumerationReflection.new(part_id, options, self)
63
71
  self.reflections.merge! part_id => reflection
64
72
 
65
- name = part_id.to_s
66
- class_name = reflection.class_name
67
- foreign_key = reflection.foreign_key
68
- failure = options[:on_lookup_failure]
69
- empty_name = options[:permit_empty_name]
73
+ name = part_id.to_s
74
+ class_name = reflection.class_name
75
+ foreign_key = reflection.foreign_key
76
+ failure = options[:on_lookup_failure]
77
+ empty_name = options[:permit_empty_name]
78
+ create_scope = options[:create_scope]
70
79
 
71
- module_eval <<-end_eval
80
+ module_eval( <<-end_eval, __FILE__, __LINE__ )
72
81
  def #{name}
73
82
  rval = #{class_name}.lookup_id(self.#{foreign_key})
74
83
  if rval.nil? && #{!failure.nil?}
@@ -120,6 +129,15 @@ module ActiveRecord
120
129
  private set_default_method
121
130
  end
122
131
 
132
+ unless create_scope == false
133
+ module_eval( <<-end_eval, __FILE__, __LINE__)
134
+ scope :with_#{name}, lambda { |*args|
135
+ ids = args.map{|arg| #{class_name}[arg] }
136
+ where(:#{foreign_key} => ids)
137
+ }
138
+ end_eval
139
+ end
140
+
123
141
  end #has_enumerated
124
142
 
125
143
  end #module MacroMethods
metadata CHANGED
@@ -1,10 +1,15 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: power_enum
3
- version: !ruby/object:Gem::Version
4
- version: 0.5.3
3
+ version: !ruby/object:Gem::Version
4
+ hash: 3
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 5
9
+ - 4
10
+ version: 0.5.4
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Trevor Squires
9
14
  - Pivotal Labs
10
15
  - Arthur Shagall
@@ -12,82 +17,98 @@ authors:
12
17
  autorequire:
13
18
  bindir: bin
14
19
  cert_chain: []
15
- date: 2012-01-17 00:00:00.000000000Z
16
- dependencies:
17
- - !ruby/object:Gem::Dependency
18
- name: rails
19
- requirement: &26849560 !ruby/object:Gem::Requirement
20
+
21
+ date: 2012-01-29 00:00:00 Z
22
+ dependencies:
23
+ - !ruby/object:Gem::Dependency
24
+ requirement: &id001 !ruby/object:Gem::Requirement
20
25
  none: false
21
- requirements:
22
- - - ! '>='
23
- - !ruby/object:Gem::Version
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
24
34
  version: 3.0.0
25
- type: :runtime
35
+ version_requirements: *id001
36
+ name: rails
26
37
  prerelease: false
27
- version_requirements: *26849560
28
- - !ruby/object:Gem::Dependency
29
- name: jeweler
30
- requirement: &26848800 !ruby/object:Gem::Requirement
38
+ type: :runtime
39
+ - !ruby/object:Gem::Dependency
40
+ requirement: &id002 !ruby/object:Gem::Requirement
31
41
  none: false
32
- requirements:
33
- - - ! '>='
34
- - !ruby/object:Gem::Version
35
- version: '0'
36
- type: :development
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ version_requirements: *id002
50
+ name: jeweler
37
51
  prerelease: false
38
- version_requirements: *26848800
39
- - !ruby/object:Gem::Dependency
40
- name: rspec
41
- requirement: &26848100 !ruby/object:Gem::Requirement
42
- none: false
43
- requirements:
44
- - - ! '>='
45
- - !ruby/object:Gem::Version
46
- version: '0'
47
52
  type: :development
48
- prerelease: false
49
- version_requirements: *26848100
50
- - !ruby/object:Gem::Dependency
51
- name: sqlite3
52
- requirement: &26847160 !ruby/object:Gem::Requirement
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: &id003 !ruby/object:Gem::Requirement
53
55
  none: false
54
- requirements:
55
- - - ! '>='
56
- - !ruby/object:Gem::Version
57
- version: '0'
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ version_requirements: *id003
64
+ name: rspec
65
+ prerelease: false
58
66
  type: :development
67
+ - !ruby/object:Gem::Dependency
68
+ requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ hash: 3
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ version_requirements: *id004
78
+ name: sqlite3
59
79
  prerelease: false
60
- version_requirements: *26847160
61
- - !ruby/object:Gem::Dependency
62
- name: genspec
63
- requirement: &26846180 !ruby/object:Gem::Requirement
80
+ type: :development
81
+ - !ruby/object:Gem::Dependency
82
+ requirement: &id005 !ruby/object:Gem::Requirement
64
83
  none: false
65
- requirements:
66
- - - =
67
- - !ruby/object:Gem::Version
84
+ requirements:
85
+ - - "="
86
+ - !ruby/object:Gem::Version
87
+ hash: 21
88
+ segments:
89
+ - 0
90
+ - 2
91
+ - 1
68
92
  version: 0.2.1
69
- type: :development
93
+ version_requirements: *id005
94
+ name: genspec
70
95
  prerelease: false
71
- version_requirements: *26846180
72
- description: ! 'Power Enum allows you to treat instances of your ActiveRecord models
73
- as though they were an enumeration of values.
74
-
75
- It allows you to cleanly solve many of the problems that the traditional Rails alternatives
76
- handle poorly if at all.
77
-
78
- It is particularly suitable for scenarios where your Rails application is not the
79
- only user of the database, such as
80
-
81
- when it''s used for analytics or reporting.
96
+ type: :development
97
+ description: |
98
+ Power Enum allows you to treat instances of your ActiveRecord models as though they were an enumeration of values.
99
+ It allows you to cleanly solve many of the problems that the traditional Rails alternatives handle poorly if at all.
100
+ It is particularly suitable for scenarios where your Rails application is not the only user of the database, such as
101
+ when it's used for analytics or reporting.
82
102
 
83
- '
84
103
  email: arthur.shagall@gmail.com
85
104
  executables: []
105
+
86
106
  extensions: []
87
- extra_rdoc_files:
107
+
108
+ extra_rdoc_files:
88
109
  - LICENSE
89
110
  - README.md
90
- files:
111
+ files:
91
112
  - examples/virtual_enumerations_sample.rb
92
113
  - lib/active_record/acts/enumerated.rb
93
114
  - lib/active_record/aggregations/has_enumerated.rb
@@ -105,27 +126,36 @@ files:
105
126
  - README.md
106
127
  homepage: http://github.com/albertosaurus/enumerations_mixin
107
128
  licenses: []
129
+
108
130
  post_install_message:
109
131
  rdoc_options: []
110
- require_paths:
132
+
133
+ require_paths:
111
134
  - lib
112
- required_ruby_version: !ruby/object:Gem::Requirement
135
+ required_ruby_version: !ruby/object:Gem::Requirement
113
136
  none: false
114
- requirements:
115
- - - ! '>='
116
- - !ruby/object:Gem::Version
117
- version: '0'
118
- required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ hash: 3
141
+ segments:
142
+ - 0
143
+ version: "0"
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
145
  none: false
120
- requirements:
121
- - - ! '>='
122
- - !ruby/object:Gem::Version
123
- version: '0'
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ hash: 3
150
+ segments:
151
+ - 0
152
+ version: "0"
124
153
  requirements: []
154
+
125
155
  rubyforge_project:
126
156
  rubygems_version: 1.8.10
127
157
  signing_key:
128
158
  specification_version: 3
129
- summary: Allows you to treat instances of your ActiveRecord models as though they
130
- were an enumeration of values
159
+ summary: Allows you to treat instances of your ActiveRecord models as though they were an enumeration of values
131
160
  test_files: []
161
+