Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add status_of_value search #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Now we have `status_name`, `status_color`, `status_value` and `Book.status_optio
- `#{attribute}_color` - return color for enum value from I18n file.
- `#{attribute}_value` - return raw value for enum, Now we don't need use `Book.statuses[@book.status]`.
- `Book.#{attribute}_options` - return a array list for select tag options, `<%= f.select :status, Book.status_options %>`.
- `Book.#{attribute}_of_#{option}` - return a active record relation for enum value from database.

Write I18n:

Expand Down
8 changes: 8 additions & 0 deletions lib/enumize/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ def enum(opts)
[label, k]
end
end

# Book.status_of_published
values.each do |key, val|
detect_enum_conflict!(name, "#{name}_of_#{key}", true)
define_singleton_method("#{name}_of_#{key}") do
klass.where("#{name}": val)
end
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/enumize/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Enumize
VERSION = "0.2.0"
VERSION = "0.2.1"
end