aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/enum.rb
Commit message (Collapse)AuthorAgeFilesLines
* Do not type cast twice on attribute assignmentSean Griffin2014-06-071-5/+2
| | | | | | | | | | | | | The definition of `write_attribute` in dirty checking ultimately leads to the columns calling `type_cast` on the value to perform the comparison. However, this is a potentially expensive computation that we cache when it occurs in `read_attribute`. The only case that we need the non-type-cast form is for numeric, so we pass that through as well (something I'm looking to remove in the future). This also reduces the number of places that manually access various stages in an attribute's type casting lifecycle, which will aid in one of the larger refactorings that I'm working on.
* Documentation, add examples of using an Enum scopeZac2014-05-091-2/+4
| | | When scanning the Enum API page I found it easy to miss that scopes are generated. Adding an example clarifies use and has more... *pop*...
* Ensure we are looking with string keysGodfrey Chan2014-04-071-1/+1
|
* Follow up to bbe7fe41 to fix enum leakage across classes.Godfrey Chan2014-04-071-3/+6
| | | | | | | | 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-4/+6
|
* Document that enum conditions must use the ordinal valueRafael Mendonça França2014-02-041-0/+2
| | | | [ci skip]
* `enum` now raises on "dangerous" name conflictsGodfrey Chan2014-01-291-3/+43
| | | | | | | | | 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.
* Use string as keysRafael Mendonça França2014-01-211-2/+2
|
* Store the enum values in the DEFINED_ENUM constantRafael Mendonça França2014-01-211-8/+8
| | | | | This will make simpler to compare if the values changed in the save_changed_attribute method.
* Add more tests for the dirty feature for enumsRafael Mendonça França2014-01-211-7/+19
|
* Make enum feature work with dirty methodsRafael Mendonça França2014-01-211-1/+18
| | | | | To make this possible we have to override the save_changed_attribute hook.
* Updated comment to mention the enum mapping class method [ci skip]Godfrey Chan2014-01-141-2/+3
|
* Enum mappings are now exposed via class methods instead of constants.Godfrey Chan2014-01-141-7/+10
| | | | | | | | | | | | | | | | 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/+3
| | | | | | | | | | | | | | | | | | | 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.
* Minor typo fix [ci skip]Robin Dupret2014-01-031-1/+1
|
* Whitespace [ci skip]Rafael Mendonça França2014-01-031-1/+1
|
* Building new records with enum scopes now works as expectedGodfrey Chan2014-01-031-2/+8
| | | | | | | | | | | | | | | | | | | | | | 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.
* Improve enum documentationRobin Dupret2014-01-011-1/+6
| | | | | Add a mention about the automatic generation of scopes based on the allowed values of the field on the enum documentation.
* Fix the enums writer methodsRobin Dupret2014-01-011-1/+1
| | | | | | | | | | | 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-1/+6
|
* Fix comment example in AR enum [ci skip]Carlos Antonio da Silva2013-12-231-1/+1
|
* Use the same enum definition in the docs and in the comments [ci skip]Godfrey Chan2013-12-221-6/+6
|
* Very tiny typo fix [ci skip]Robin Dupret2013-12-061-1/+1
|
* _enum_methods_module should be kept privateGodfrey Chan2013-12-051-6/+7
|
* Make clear that the enum array should not be changed once defined. [ci skip]Godfrey Chan2013-12-051-1/+12
|
* bring back constant to expose the enum mapping as HWIA.Yves Senn2013-11-061-4/+13
|
* update the AR enum docs to match the actual behavior. [ci skip]Yves Senn2013-11-061-5/+3
|
* store enum mapping using `Strings` instead of `Symbols`.Yves Senn2013-11-051-1/+2
| | | | | 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-1/+6
|
* define enum methods inside a `Module` to make them overwritable.Yves Senn2013-11-051-13/+24
|
* The enum value constant isn't used, so rm it for now.Aaron Patterson2013-11-041-17/+7
|
* stop doing the same calculation over and overAaron Patterson2013-11-041-4/+5
|
* Fix failures related to enum changesCarlos Antonio da Silva2013-11-041-6/+4
| | | | | | Commit 6e4a810f51b9ed2468b92247ef5568cc9d6dbcc0 changed the implementation from class_eval to define_method, but missed the access to the constant versus the access to the constant name.
* Replace `class_eval` with `define_method` for enumVitaly Tatarintsev2013-11-041-4/+12
| | | | https://github.com/rails/rails/commit/db41eb8a6ea88b854bf5cd11070ea4245e1639c5#commitcomment-4502683
* Move documentation around a bitDavid Heinemeier Hansson2013-11-021-4/+7
|
* Explicit mapping for enumYury Korolev2013-11-021-1/+5
|
* Fix to work on Ruby 1.9.3, example and changelog improvementsCarlos Antonio da Silva2013-11-021-5/+5
|
* Added ActiveRecord::Base#enum for declaring enum attributes where the values ↵David Heinemeier Hansson2013-11-021-0/+60
map to integers in the database, but can be queried by name