Skip to content

Commit

Permalink
Merge branch 'hotfix/0.6.10'
Browse files Browse the repository at this point in the history
  • Loading branch information
thornomad committed Sep 13, 2020
2 parents 8dc67bf + ed6dec7 commit 9c90ffc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
enumbler (0.6.9)
enumbler (0.6.10)
activerecord (>= 5.2.3, < 6.1)
activesupport (>= 5.2.3, < 6.1)

Expand Down
15 changes: 7 additions & 8 deletions lib/enumbler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ module ClassMethods
# example: `where_by` will make it `House.where_by_color(:black)`
# @param **options [Hash] additional options passed to `belongs_to`
def enumbled_to(name, scope = nil, prefix: false, scope_prefix: nil, **options)
class_name = name.to_s.classify
class_name = options.fetch(:class_name, name.to_s.classify)
enumbled_model = class_name.constantize

unless enumbled_model.respond_to?(:enumbles)
Expand All @@ -68,7 +68,7 @@ def enumbled_to(name, scope = nil, prefix: false, scope_prefix: nil, **options)
belongs_to(name, scope, **options)

define_helper_attributes(name)
define_dynamic_methods_for_enumbled_to_models(enumbled_model, prefix: prefix, scope_prefix: scope_prefix)
define_dynamic_methods_for_enumbled_to_models(name, enumbled_model, prefix: prefix, scope_prefix: scope_prefix)
rescue NameError
raise Error, "The model #{class_name} cannot be found. Uninitialized constant."
end
Expand All @@ -80,11 +80,10 @@ def enumbled_to(name, scope = nil, prefix: false, scope_prefix: nil, **options)
# @todo - we should check for naming conflicts!
# dangerous_attribute_method?(method_name)
# method_defined_within?(method_name, self, Module)
def define_dynamic_methods_for_enumbled_to_models(enumbled_model, prefix: false, scope_prefix: nil)
model_name = enumbled_model.to_s.underscore
column_name = "#{model_name}_id"
def define_dynamic_methods_for_enumbled_to_models(name, enumbled_model, prefix: false, scope_prefix: nil)
column_name = "#{name}_id"

cmethod = scope_prefix.blank? ? model_name : "#{scope_prefix}_#{model_name}"
cmethod = scope_prefix.blank? ? name : "#{scope_prefix}_#{name}"
define_singleton_method(cmethod) do |*args|
where(column_name => enumbled_model.ids_from_enumbler(args))
end
Expand All @@ -94,8 +93,8 @@ def define_dynamic_methods_for_enumbled_to_models(enumbled_model, prefix: false,
end

enumbled_model.enumbles.each do |enumble|
method_name = prefix ? "#{model_name}_#{enumble.enum}?" : "#{enumble.enum}?"
not_method_name = prefix ? "#{model_name}_not_#{enumble.enum}?" : "not_#{enumble.enum}?"
method_name = prefix ? "#{name}_#{enumble.enum}?" : "#{enumble.enum}?"
not_method_name = prefix ? "#{name}_not_#{enumble.enum}?" : "not_#{enumble.enum}?"
define_method(method_name) { self[column_name] == enumble.id }
define_method(not_method_name) { self[column_name] != enumble.id }
end
Expand Down
2 changes: 1 addition & 1 deletion lib/enumbler/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Enumbler
VERSION = '0.6.9'
VERSION = '0.6.10'
end
14 changes: 14 additions & 0 deletions spec/enumbler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
t.references :color, foreign_key: true, null: false
end

create_table :skies, force: true do |t|
t.references :pretty_color, null: false, foreign_key: { to_table: 'colors' }
end

create_table :palettes, force: true do |t|
t.string :label, null: false, index: { unique: true }
end
Expand Down Expand Up @@ -76,6 +80,10 @@ class Palette < ApplicationRecord
has_many :colors, through: :color_palettes
end

class Sky < ApplicationRecord
enumbled_to :pretty_color, class_name: 'Color'
end

# -----------------------------------------------------------------------------
# Here are the expectations. Note that `Color.seed_the_enumbler!` needs to be
# run to seed the database. We are using transactions here so we need to run it
Expand Down Expand Up @@ -164,11 +172,17 @@ class Palette < ApplicationRecord
it 'raises an error when the class does not exist' do
expect { House.enumbled_to(:bob) }.to raise_error(Enumbler::Error, /cannot be found/)
end

it 'raises an error when the class is not enumbled' do
class_double('MyFriendBob').as_stubbed_const
expect { House.enumbled_to(:my_friend_bob) }.to raise_error(Enumbler::Error, /not have any enumbles/)
end

it 'uses the `class_name` option when present' do
sky = Sky.create!(pretty_color: Color.black)
expect(Sky.pretty_color(:black)).to contain_exactly(sky)
end

context 'when adding adds searchable scoped class method' do
it 'queries based on the enumbler' do
house = House.create! color: Color.black
Expand Down

0 comments on commit 9c90ffc

Please sign in to comment.