Skip to content

Commit

Permalink
Merge pull request #71 from lucascaton/active-record-auto-extend
Browse files Browse the repository at this point in the history
Make ActiveRecord classes extend `EnumerateIt` automatically
  • Loading branch information
lucascaton authored Aug 21, 2017
2 parents b4ef3dd + ac44f62 commit 5feca81
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 47 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
enumerate_it (1.5.0)
enumerate_it (1.6.0)
activesupport (>= 3.0.0)

GEM
Expand Down Expand Up @@ -80,4 +80,4 @@ DEPENDENCIES
wwtd

BUNDLED WITH
1.15.3
1.15.4
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,7 @@ Add the gem to your Gemfile:
gem 'enumerate_it'
```

Run the install generator:

```bash
rails generate enumerate_it:install
```

There is also a Rails Generator which generates enumerations and their locale files:
You can use a Rails generator, which generates enumerations and their locale files:

```bash
rails generate enumerate_it:enum --help
Expand Down Expand Up @@ -183,10 +177,17 @@ The `sort_by` method accepts one of the following values:

## Using enumerations

The cool part is that you can use these enumerations with any class, be it an `ActiveRecord`
instance or not.
The cool part is that you can use these enumerations with any class:

```ruby
# ActiveRecord instance
class Person < ApplicationRecord
has_enumeration_for :relationship_status
end
```

```ruby
# Non-ActiveRecord instance
class Person
extend EnumerateIt
attr_accessor :relationship_status
Expand Down Expand Up @@ -231,7 +232,7 @@ This will create:
* A helper method for each enumeration option, if you pass the `create_helpers` option as `true`:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus, create_helpers: true
end

Expand All @@ -249,7 +250,7 @@ This will create:
option. This can be useful when two or more of the enumerations used share the same constants:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_helpers: { prefix: true }
end
Expand Down Expand Up @@ -279,12 +280,12 @@ This will create:

class Single
def saturday_night
'Party Hard!'
'Party hard!'
end
end
end

class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_helpers: { polymorphic: true }
end
Expand All @@ -296,13 +297,13 @@ This will create:

p.relationship_status = RelationshipStatus::SINGLE
p.relationship_status_object.saturday_night
#=> 'Party Hard!'
#=> 'Party hard!'
```

You can also change the suffix `_object`, using the `suffix` option:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_helpers: { polymorphic: { suffix: '_mode' } }
end
Expand All @@ -324,7 +325,7 @@ This will create:
* A scope method for each enumeration option if you pass the `create_scopes` option as `true`:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus, create_scopes: true
end

Expand All @@ -335,7 +336,7 @@ This will create:
The `:create_scopes` also accepts `prefix` option.

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status,
with: RelationshipStatus, create_scopes: { prefix: true }
end
Expand All @@ -347,7 +348,7 @@ This will create:
`validates_inclusion_of`):

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus
end

Expand All @@ -362,7 +363,7 @@ This will create:
`validates_presence_of` and you pass the `required` options as `true`):

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, required: true
end

Expand All @@ -376,7 +377,7 @@ This will create:
If you pass the `skip_validation` option as `true`, it will not create any validations:

```ruby
class Person < ActiveRecord::Base
class Person < ApplicationRecord
has_enumeration_for :relationship_status, with: RelationshipStatus, skip_validation: true
end

Expand All @@ -385,8 +386,7 @@ This will create:
#=> true
```

Remember that you can add validations to any kind of class and not only to those derived from
`ActiveRecord::Base`.
Remember that you can add validations to any kind of class and not only `ActiveRecord` ones.

## FAQ

Expand Down
2 changes: 2 additions & 0 deletions lib/enumerate_it.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ def self.extended(receiver)
receiver.extend ClassMethods
end
end

ActiveSupport.on_load(:active_record) { ActiveRecord::Base.extend EnumerateIt }
2 changes: 1 addition & 1 deletion lib/enumerate_it/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module EnumerateIt
VERSION = '1.5.0'.freeze
VERSION = '1.6.0'.freeze
end
8 changes: 0 additions & 8 deletions lib/generators/enumerate_it/install/USAGE

This file was deleted.

11 changes: 0 additions & 11 deletions lib/generators/enumerate_it/install/install_generator.rb

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions spec/enumerate_it_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ class Polymorphic
Object.send :remove_const, 'TestClassWithScope' if defined?(TestClassWithScope)

class TestClassWithScope < ActiveRecord::Base
extend EnumerateIt
has_enumeration_for :foobar, with: TestEnumeration, create_scopes: true
end
end
Expand Down Expand Up @@ -290,7 +289,6 @@ class GenericClass
context 'with :prefix option' do
before do
class OtherTestClass < ActiveRecord::Base
extend EnumerateIt
has_enumeration_for :foobar, with: TestEnumerationWithReservedWords,
create_scopes: { prefix: true }
end
Expand Down

0 comments on commit 5feca81

Please sign in to comment.