aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/enum_test.rb
Commit message (Collapse)AuthorAgeFilesLines
* Prevent scope named same as a ActiveRecord::Relation instance method.Chen Kinnrot2017-11-281-0/+18
| | | | | | | | | Due to inconsistent behavior when chaining scopes and one scope named after a Relation method Validation code added in 2 places: - scope, to prevent problematic scope names. - enum, cause it tries to auto define scope.
* Load both `:authors` and `:author_addresses` to keep data integrityYasuo Honda2017-08-221-1/+1
| | | | | | | | | | | | | | | | | | | | `:authors` has a foreign key to `:author_addresses`. If only `:authors` fixture loaded into the database which supports foreign key and checks the existing data when enabling foreien keys like Oracle, it raises the following error `ORA-02298: cannot validate (ARUNIT.FK_RAILS_94423A17A3) - parent keys not found` It is because there is no parent data exists in `author_addresses` table. Here are how other database with foreign key support works: - MySQL does not check the existing data when enabling foreign key by `foreign_key_checks=1` https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_foreign_key_checks > Setting foreign_key_checks to 1 does not trigger a scan of the existing table data. Therefore, rows added to the table while foreign_key_checks=0 will not be verified for consistency. - PostgreSQL database itself has a feature to check existing data when enabling foreign key and discussed at #27636, which is reverted.
* Fix test "_before_type_cast" for enum (#29004)Ryuta Kamizono2017-08-161-6/+8
| | | Since c51f9b61 changed the "_before_type_cast" expectation for enum.
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* Revert "Merge pull request #29540 from kirs/rubocop-frozen-string"Matthew Draper2017-07-021-1/+0
| | | | | This reverts commit 3420a14590c0e6915d8b6c242887f74adb4120f9, reversing changes made to afb66a5a598ce4ac74ad84b125a5abf046dcf5aa.
* Enforce frozen string in RubocopKir Shatrov2017-07-011-0/+1
|
* Prevent making bind param if casted value is nilRyuta Kamizono2017-05-311-0/+2
| | | | | | | | If casted value is nil, generated SQL should be `IS NULL`. But currently it is generated as `= NULL`. To prevent this behavior, avoid making bind param if casted value is nil. Fixes #28945.
* Refactor enum to use `value` instead of `label` in the scopeRyuta Kamizono2017-05-071-2/+2
|
* Add type caster to `RuntimeReflection#alias_name`Jon Moss2017-05-021-1/+4
| | | | | Since we have been using this `Arel::Table` since 111ccc832bc977b15af12c14e7ca078dad2d4373, in order to properly handle queries, it's important that we properly type cast arguments.
* Add tests for ActiveRecord::Enum#enum when suffix specifiedYosuke Kabuto2016-09-121-0/+40
| | | | Make name of attribute medium instead of normal
* applies new string literal convention in activerecord/testXavier Noria2016-08-061-19/+19
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Fix `enum` with `alias_attribute`Ryuta Kamizono2016-07-311-0/+16
| | | | Fixes #25892.
* return correct type from `EnumType`yuuji.yaginuma2016-06-241-0/+4
|
* Use the database type to deserialize enumSean Griffin2016-01-231-0/+10
| | | | | | | | | | | | This fixes incorrect assumptions made by e991c7b that we can assume the DB is already casting the value for us. The enum type needs additional information to perform casting, and needs a subtype. I've opted not to call `super` in `cast`, as we have a known set of types which we accept there, and the subtype likely doesn't accept them (symbol -> integer doesn't make sense) Close #23190
* use `assert_not` instead of `refute` as mentioned in our guides.Yves Senn2015-08-131-13/+13
| | | | | | | | | | | As described in the "Follow Coding Conventions" section in our contribution guide (http://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions) we favor `assert_not` over `refute`. While we don't usually make stylistic changes on it's own I opted to do it in this case. The reason being that test cases are usually copied as a starting point for new tests. This results in a spread of `refute` in files that have been using it already.
* Use default model enum in fixtures if not definedeileencodes2015-07-021-0/+6
| | | | | | | | | | | After 908cfef was introduced fixtures that did not set an enum would return nil instead of the default enum value. The fixtures should assume the default if a different enum is not defined. The change checks first if the enum is defined in the fixture before setting it based on the fixture.
* Add enum prefix/suffix option to enum definitionIgor Kapkov2015-06-121-0/+50
| | | | Fixes #17511 and #17415
* Resolve enums in test fixturesGeorge Claghorn2015-05-271-30/+30
| | | | | | | | | | | | | | | | | | | | | | | Currently, values for columns backing Active Record enums must be specified as integers in test fixtures: awdr: title: "Agile Web Development with Rails" status: 2 rfr: title: "Ruby for Rails" status: <%= Book.statuses[:proposed] %> This is potentially confusing, since enum values are typically specified as symbols or strings in application code. To resolve the confusion, this change permits the use of symbols or strings to specify enum values: awdr: status: :published It is compatible with fixtures that specify enum values as integers.
* add ActiveRecord::Enum testcase about multiple enums declaring at a timekeepcosmos2015-03-051-0/+14
|
* Error message testing fixFranky W2015-02-201-2/+4
| | | | | | | | | The testing of error messages have been implemented wrongly a few times. This is an attempt to fix it. For example, some of these test should have failed with the new code. The reason they are not failling with the new string is the fact they were not being tested beforehand.
* Mention `where.not` in the exampleGodfrey Chan2015-02-141-0/+4
| | | | | | | ...so it doesn't look like you *have* to use SQL strings for that case (not anymore!). Would like to replace the SQL string example with something that you cannot do with the "normal" query API, but I could not come up with a short, realistic example. Suggestions welcome!
* Fixed a bug where NULLs are casted into the first enum valueGodfrey Chan2015-02-131-3/+8
|
* Partially revert ae33aeaGodfrey Chan2015-02-131-4/+0
| | | | | Using enum names in SQL strings doesn't actually work, the test was wrong (fixed in 3dfd1ba).
* Break up these testsGodfrey Chan2015-02-131-4/+23
| | | | Make sure we have coverage for both the find/build cases
* oops, 'format' is not a real enumGodfrey Chan2015-02-131-8/+8
|
* Enums should be referred to by symbolsGodfrey Chan2015-02-131-1/+14
| | | | | Also updated the documentation about the new ability to query them normally, and added test to make sure they work!
* Refactor enum to be defined in terms of the attributes APISean Griffin2015-02-111-1/+16
| | | | | | | | | | | In addition to cleaning up the implementation, this allows type casting behavior to be applied consistently everywhere. (#where for example). A good example of this was the previous need for handling value to key conversion in the setter, because the number had to be passed to `where` directly. This is no longer required, since we can just pass the string along to where. (It's left around for backwards compat) Fixes #18387
* Enums shouldn't ruin people's anniversariesGodfrey Chan2014-09-041-1/+2
| | | | | | | | Added a few more methods on Module/Class to the dangerous class methods blacklist. (Technically, allocate and new are already protected currently because we happen to redefine them in the current implantation.) Closes #16792
* Follow up to bbe7fe41 to fix enum leakage across classes.Godfrey Chan2014-04-071-7/+28
| | | | | | | | The original attempt didn't really fix the problem and wasn't testing the problematic area. This commit corrected those issues in the original commit. Also removed the private `enum_mapping_for` method. As `defined_enums` is now a method, this method doesn't provide much value anymore.
* make enums distinct per classEvan Whalen2014-04-071-0/+15
|
* Block a few default Class methods as scope name.Arthur Neves2014-04-031-0/+1
| | | | | | Add tests to make sure scopes cannot be create with names such as: private, protected, public. Make sure enum values don't collide with those methods too.
* Fix a bug affecting validations of enum attributesTheMonster2014-02-271-0/+27
| | | | | | | | | | | | | | This fixes a bug where any enum attribute of a model would be evaluated always as 0 when calling the database on validations. This fix converts the value of the enum attribute to its integer value rather than the string before building the relation as the bug occured when the string finally gets converted to integer using string.to_i which converts it to 0. [Vilius Luneckas, Ahmed AbouElhamayed]
* Remove unused variable.Tim Fenney2014-01-311-1/+1
|
* `enum` now raises on "dangerous" name conflictsGodfrey Chan2014-01-291-0/+59
| | | | | | | | | Dangerous name conflicts includes instance or class method conflicts with methods defined within `ActiveRecord::Base` but not its ancestors, as well as conflicts with methods generated by other enums on the same class. Fixes #13389.
* Remove unused assignment to fix warnings in enum test.Vipul A M2014-01-241-1/+0
|
* Add more tests for the dirty feature for enumsRafael Mendonça França2014-01-211-0/+32
|
* Make enum feature work with dirty methodsRafael Mendonça França2014-01-211-0/+40
| | | | | To make this possible we have to override the save_changed_attribute hook.
* Enum mappings are now exposed via class methods instead of constants.Godfrey Chan2014-01-141-3/+3
| | | | | | | | | | | | | | | | Example: class Conversation < ActiveRecord::Base enum status: [ :active, :archived ] end Before: Conversation::STATUS # => { "active" => 0, "archived" => 1 } After: Conversation.statuses # => { "active" => 0, "archived" => 1 }
* use enum labels as form values. Achieved by `_before_type_cast`.Yves Senn2014-01-111-0/+4
| | | | | | | | | | | | | | | | | | | Closes #13650, #13672 This is an alternate implementation to solve #13650. Currently form fields contain the enum value (eg. "1"). This breaks because the setter `enum=` expects the label (eg. "active"). ActiveRecord::Enum allows you to use labels in your application but store numbers. We should make sure that all parts after AR are dealing with labels and not the underlying mapping to a number. This patch defines `_before_type_cast` on every enum column to return the label. This method is later used to fetch the value to display in form fields. I deliberately copied the implementation of the enum getter instead of delegating to it. This allows you to overwrite the getter and for example return a `Value Object` but have it still work for form fields.
* Building new records with enum scopes now works as expectedGodfrey Chan2014-01-031-6/+7
| | | | | | | | | | | | | | | | | | | | | | Previously, this would give an `ArgumentError`: class Issue < ActiveRecord::Base enum :status, [:open, :finished] end Issue.open.build # => ArgumentError: '0' is not a valid status Issue.open.create # => ArgumentError: '0' is not a valid status PR #13542 muted the error, but the issue remains. This commit fixes the issue by allowing the enum value to be written directly via the setter: Issue.new.status = 0 # This now sets status to :open Assigning a value directly via the setter like this is not part of the documented public API, so users should not rely on this behavior. Closes #13530.
* Fix the enums writer methodsRobin Dupret2014-01-011-1/+10
| | | | | | | | | | | Previously, the writer methods would simply check whether the passed argument was the symbol representing the integer value of an enum field. Therefore, it was not possible to specify the numeric value itself but the dynamically defined scopes generate where clauses relying on this kind of values so a chained call to a method like `find_or_initialize_by` would trigger an `ArgumentError`. Reference #13530
* Add the ability to nullify the `enum` columnAmr Tamimi2014-01-011-0/+15
|
* bring back constant to expose the enum mapping as HWIA.Yves Senn2013-11-061-0/+6
|
* store enum mapping using `Strings` instead of `Symbols`.Yves Senn2013-11-051-2/+7
| | | | | This allows to assign both `String` and `Symbol` values to the enum without having to call `to_sym`, which is a security problem.
* direct enum assignment rasies ArgumentError for unknown values.Yves Senn2013-11-051-0/+12
|
* define enum methods inside a `Module` to make them overwritable.Yves Senn2013-11-051-0/+5
|
* The enum value constant isn't used, so rm it for now.Aaron Patterson2013-11-041-10/+0
|
* fix copy & paste test-case naming. [ci skip]Yves Senn2013-11-041-1/+1
|
* Explicit mapping for enumYury Korolev2013-11-021-0/+8
|
* Add a test case for the scope enum addsDavid Heinemeier Hansson2013-11-021-0/+4
|