Skip to content

Commit

Permalink
Merge branch 'hotfix/0.6.7'
Browse files Browse the repository at this point in the history
  • Loading branch information
thornomad committed Aug 5, 2020
2 parents 4b20fe7 + 6fdf5cc commit 45817fc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 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.6)
enumbler (0.6.7)
activerecord (>= 5.2.3, < 6.1)
activesupport (>= 5.2.3, < 6.1)

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ Color.black.black? # => true
Color.black.is_black # => true
Color.white.not_black? # => true

Color.all.any_black? # => true
Color.where.not(id: Color::BLACK).any_black? # => false

# Get attributes without hitting the database
Color.black(:id) # => 1
Color.black(:enum) # => :black
Expand Down
6 changes: 6 additions & 0 deletions lib/enumbler/enabler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,12 @@ def define_dynamic_methods_and_constants_for_enumbled_model(enumble)
rescue NoMethodError
raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
end

define_singleton_method("any_#{enumble.enum}?") do
where(id: enumble.id).exists?
rescue NoMethodError
raise Enumbler::Error, "The attribute #{attr} is not supported on this Enumble."
end
end

# I accidentally forgot to provide an id one time and it was confusing as
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.6'
VERSION = '0.6.7'
end
33 changes: 33 additions & 0 deletions spec/enumbler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
create_table :houses, force: true do |t|
t.references :color, foreign_key: true, null: false
end

create_table :palettes, force: true do |t|
t.string :label, null: false, index: { unique: true }
end

create_table :color_palettes, force: true do |t|
t.references :color, foreign_key: true, null: false
t.references :palette, foreign_key: true, null: false
end
end

class ApplicationRecord < ActiveRecord::Base
Expand Down Expand Up @@ -57,6 +66,16 @@ class ModelWithoutTable < ApplicationRecord
enumble :without_hope, 1
end

class ColorPalette < ApplicationRecord
belongs_to :palette
enumbled_to :color
end

class Palette < ApplicationRecord
has_many :color_palettes
has_many :colors, through: :color_palettes
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 @@ -121,6 +140,20 @@ class ModelWithoutTable < ApplicationRecord
ModelWithoutTable.enumble(:test, 2, bob: 'ok')
end
end

it 'queries the collection', :seed do
expect(Color.all.any_black?).to be true
expect(Color.all.where.not(id: Color::BLACK).any_black?).to be false

palette = Palette.new(label: 'Greys')
palette.colors << Color.black
palette.colors << Color.white
palette.save!

expect(palette.colors.size).to eq 2
expect(palette.colors.any_black?).to be true
expect(palette.colors.any_dark_brown?).to be false
end
end

describe '.enumbled_to', :seed do
Expand Down

0 comments on commit 45817fc

Please sign in to comment.