diff --git a/Gemfile.lock b/Gemfile.lock index df0fce7..56a8f0a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - enumerate_it (1.5.0) + enumerate_it (1.6.0) activesupport (>= 3.0.0) GEM @@ -80,4 +80,4 @@ DEPENDENCIES wwtd BUNDLED WITH - 1.15.3 + 1.15.4 diff --git a/README.md b/README.md index da24a9d..5b7ff3d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/lib/enumerate_it.rb b/lib/enumerate_it.rb index 44677ac..703ea85 100644 --- a/lib/enumerate_it.rb +++ b/lib/enumerate_it.rb @@ -11,3 +11,5 @@ def self.extended(receiver) receiver.extend ClassMethods end end + +ActiveSupport.on_load(:active_record) { ActiveRecord::Base.extend EnumerateIt } diff --git a/lib/enumerate_it/version.rb b/lib/enumerate_it/version.rb index 1b29fc7..1b8408f 100644 --- a/lib/enumerate_it/version.rb +++ b/lib/enumerate_it/version.rb @@ -1,3 +1,3 @@ module EnumerateIt - VERSION = '1.5.0'.freeze + VERSION = '1.6.0'.freeze end diff --git a/lib/generators/enumerate_it/install/USAGE b/lib/generators/enumerate_it/install/USAGE deleted file mode 100644 index 54039a6..0000000 --- a/lib/generators/enumerate_it/install/USAGE +++ /dev/null @@ -1,8 +0,0 @@ -Description: - Creates an initializer file for EnumerateIt - -Example: - rails generate enumerate_it:install - - This will create: - config/initializers/enumerate_it.rb diff --git a/lib/generators/enumerate_it/install/install_generator.rb b/lib/generators/enumerate_it/install/install_generator.rb deleted file mode 100644 index e01126f..0000000 --- a/lib/generators/enumerate_it/install/install_generator.rb +++ /dev/null @@ -1,11 +0,0 @@ -module EnumerateIt - module Generators - class InstallGenerator < Rails::Generators::Base - source_root File.expand_path('../templates', __FILE__) - - def copy_initializer_file - template 'enumerate_it_initializer.rb', File.join('config/initializers/', 'enumerate_it.rb') - end - end - end -end diff --git a/lib/generators/enumerate_it/install/templates/enumerate_it_initializer.rb b/lib/generators/enumerate_it/install/templates/enumerate_it_initializer.rb deleted file mode 100644 index a7befac..0000000 --- a/lib/generators/enumerate_it/install/templates/enumerate_it_initializer.rb +++ /dev/null @@ -1 +0,0 @@ -ActiveRecord::Base.extend EnumerateIt diff --git a/spec/enumerate_it_spec.rb b/spec/enumerate_it_spec.rb index 20060a8..5394f27 100644 --- a/spec/enumerate_it_spec.rb +++ b/spec/enumerate_it_spec.rb @@ -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 @@ -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