diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-06-12 07:02:24 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-06-12 07:02:24 -0600 |
commit | 0158f9261f0f3d7be5bb5974ceef96bcf862766a (patch) | |
tree | ff49f157cec90482f8593eee71d29394cfb2704f | |
parent | 40ffcd88a6a3129d8b9eb9f4dc077d8fd00aeb54 (diff) | |
parent | 09bef7684a5ddd0e37d8bed38858a9fa866e1df2 (diff) | |
download | rails-0158f9261f0f3d7be5bb5974ceef96bcf862766a.tar.gz rails-0158f9261f0f3d7be5bb5974ceef96bcf862766a.tar.bz2 rails-0158f9261f0f3d7be5bb5974ceef96bcf862766a.zip |
Merge pull request #19813 from igas/enum-prefix
Add prefix option to enum definition
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/enum.rb | 42 | ||||
-rw-r--r-- | activerecord/test/cases/enum_test.rb | 50 | ||||
-rw-r--r-- | activerecord/test/fixtures/books.yml | 4 | ||||
-rw-r--r-- | activerecord/test/models/book.rb | 4 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 4 |
6 files changed, 104 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 6680ef4bd2..bf21851ed0 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Add `:enum_prefix`/`:enum_suffix` option to `enum` definition. + + Fixes #17511 and #17415 + + *Igor Kapkov* + * Correctly handle decimal arrays with defaults in the schema dumper. Fixes #20515. diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 2b99899e42..ad33c84dbc 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -75,6 +75,22 @@ module ActiveRecord # # Conversation.where("status <> ?", Conversation.statuses[:archived]) # + # You can use <tt>:enum_prefix</tt>/<tt>:enum_suffix</tt> option then you need + # to define multiple enums with same values. If option value is <tt>true</tt>, + # the methods are prefixed/suffixed with the name of the enum. + # + # class Invoice < ActiveRecord::Base + # enum verification: [:done, :fail], enum_prefix: true + # end + # + # It is also possible to supply a custom prefix. + # + # class Invoice < ActiveRecord::Base + # enum verification: [:done, :fail], enum_prefix: :verification_status + # end + # + # Note that <tt>:enum_prefix</tt>/<tt>:enum_postfix</tt> are reserved keywords + # and can not be used as an enum name. module Enum def self.extended(base) # :nodoc: @@ -121,6 +137,8 @@ module ActiveRecord def enum(definitions) klass = self + enum_prefix = definitions.delete(:enum_prefix) + enum_suffix = definitions.delete(:enum_suffix) definitions.each do |name, values| # statuses = { } enum_values = ActiveSupport::HashWithIndifferentAccess.new @@ -138,19 +156,31 @@ module ActiveRecord _enum_methods_module.module_eval do pairs = values.respond_to?(:each_pair) ? values.each_pair : values.each_with_index pairs.each do |value, i| + if enum_prefix == true + prefix = "#{name}_" + elsif enum_prefix + prefix = "#{enum_prefix}_" + end + if enum_suffix == true + suffix = "_#{name}" + elsif enum_suffix + suffix = "_#{enum_suffix}" + end + + value_method_name = "#{prefix}#{value}#{suffix}" enum_values[value] = i # def active?() status == 0 end - klass.send(:detect_enum_conflict!, name, "#{value}?") - define_method("#{value}?") { self[name] == value.to_s } + klass.send(:detect_enum_conflict!, name, "#{value_method_name}?") + define_method("#{value_method_name}?") { self[name] == value.to_s } # def active!() update! status: :active end - klass.send(:detect_enum_conflict!, name, "#{value}!") - define_method("#{value}!") { update! name => value } + klass.send(:detect_enum_conflict!, name, "#{value_method_name}!") + define_method("#{value_method_name}!") { update! name => value } # scope :active, -> { where status: 0 } - klass.send(:detect_enum_conflict!, name, value, true) - klass.scope value, -> { klass.where name => value } + klass.send(:detect_enum_conflict!, name, value_method_name, true) + klass.scope value_method_name, -> { klass.where name => value } end end defined_enums[name.to_s] = enum_values diff --git a/activerecord/test/cases/enum_test.rb b/activerecord/test/cases/enum_test.rb index 3641826daa..769b171717 100644 --- a/activerecord/test/cases/enum_test.rb +++ b/activerecord/test/cases/enum_test.rb @@ -14,16 +14,26 @@ class EnumTest < ActiveRecord::TestCase assert_not @book.proposed? assert @book.read? + assert @book.in_english? + assert @book.author_visibility_visible? + assert @book.illustrator_visibility_visible? + assert @book.with_medium_font_size? end test "query state with strings" do assert_equal "published", @book.status assert_equal "read", @book.read_status + assert_equal "english", @book.language + assert_equal "visible", @book.author_visibility + assert_equal "visible", @book.illustrator_visibility end test "find via scope" do assert_equal @book, Book.published.first assert_equal @book, Book.read.first + assert_equal @book, Book.in_english.first + assert_equal @book, Book.author_visibility_visible.first + assert_equal @book, Book.illustrator_visibility_visible.first end test "find via where with values" do @@ -72,6 +82,10 @@ class EnumTest < ActiveRecord::TestCase test "update by declaration" do @book.written! assert @book.written? + @book.in_english! + assert @book.in_english? + @book.author_visibility_visible! + assert @book.author_visibility_visible? end test "update by setter" do @@ -96,42 +110,61 @@ class EnumTest < ActiveRecord::TestCase test "enum changed attributes" do old_status = @book.status + old_language = @book.language @book.status = :proposed + @book.language = :spanish assert_equal old_status, @book.changed_attributes[:status] + assert_equal old_language, @book.changed_attributes[:language] end test "enum changes" do old_status = @book.status + old_language = @book.language @book.status = :proposed + @book.language = :spanish assert_equal [old_status, 'proposed'], @book.changes[:status] + assert_equal [old_language, 'spanish'], @book.changes[:language] end test "enum attribute was" do old_status = @book.status + old_language = @book.language @book.status = :published + @book.language = :spanish assert_equal old_status, @book.attribute_was(:status) + assert_equal old_language, @book.attribute_was(:language) end test "enum attribute changed" do @book.status = :proposed + @book.language = :french assert @book.attribute_changed?(:status) + assert @book.attribute_changed?(:language) end test "enum attribute changed to" do @book.status = :proposed + @book.language = :french assert @book.attribute_changed?(:status, to: 'proposed') + assert @book.attribute_changed?(:language, to: 'french') end test "enum attribute changed from" do old_status = @book.status + old_language = @book.language @book.status = :proposed + @book.language = :french assert @book.attribute_changed?(:status, from: old_status) + assert @book.attribute_changed?(:language, from: old_language) end test "enum attribute changed from old status to new status" do old_status = @book.status + old_language = @book.language @book.status = :proposed + @book.language = :french assert @book.attribute_changed?(:status, from: old_status, to: 'proposed') + assert @book.attribute_changed?(:language, from: old_language, to: 'french') end test "enum didn't change" do @@ -201,11 +234,15 @@ class EnumTest < ActiveRecord::TestCase test "building new objects with enum scopes" do assert Book.written.build.written? assert Book.read.build.read? + assert Book.in_spanish.build.in_spanish? + assert Book.illustrator_visibility_invisible.build.illustrator_visibility_invisible? end test "creating new objects with enum scopes" do assert Book.written.create.written? assert Book.read.create.read? + assert Book.in_spanish.create.in_spanish? + assert Book.illustrator_visibility_invisible.create.illustrator_visibility_invisible? end test "_before_type_cast returns the enum label (required for form fields)" do @@ -355,4 +392,17 @@ class EnumTest < ActiveRecord::TestCase book2 = klass.single.create! assert book2.single? end + + test "query state by predicate with prefix" do + assert @book.author_visibility_visible? + assert_not @book.author_visibility_invisible? + assert @book.illustrator_visibility_visible? + assert_not @book.illustrator_visibility_invisible? + end + + test "query state by predicate with custom prefix" do + assert @book.in_english? + assert_not @book.in_spanish? + assert_not @book.in_french? + end end diff --git a/activerecord/test/fixtures/books.yml b/activerecord/test/fixtures/books.yml index 380dd3dfca..93cfabd61c 100644 --- a/activerecord/test/fixtures/books.yml +++ b/activerecord/test/fixtures/books.yml @@ -5,6 +5,10 @@ awdr: format: "paperback" status: :published read_status: :read + language: :english + author_visibility: :visible + illustrator_visibility: :visible + font_size: :medium rfr: author_id: 1 diff --git a/activerecord/test/models/book.rb b/activerecord/test/models/book.rb index 2170018068..24bfe47bbf 100644 --- a/activerecord/test/models/book.rb +++ b/activerecord/test/models/book.rb @@ -10,6 +10,10 @@ class Book < ActiveRecord::Base enum status: [:proposed, :written, :published] enum read_status: {unread: 0, reading: 2, read: 3} enum nullable_status: [:single, :married] + enum language: [:english, :spanish, :french], enum_prefix: :in + enum author_visibility: [:visible, :invisible], enum_prefix: true + enum illustrator_visibility: [:visible, :invisible], enum_prefix: true + enum font_size: [:small, :medium, :large], enum_prefix: :with, enum_suffix: true def published! super diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 8b010f265c..dffccc9326 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -100,6 +100,10 @@ ActiveRecord::Schema.define do t.column :status, :integer, default: 0 t.column :read_status, :integer, default: 0 t.column :nullable_status, :integer + t.column :language, :integer, default: 0 + t.column :author_visibility, :integer, default: 0 + t.column :illustrator_visibility, :integer, default: 0 + t.column :font_size, :integer, default: 0 end create_table :booleans, force: true do |t| |