diff --git a/README.md b/README.md index 6f84cad..1877b04 100644 --- a/README.md +++ b/README.md @@ -163,17 +163,17 @@ mode, you can use the `sort_by` class method. class RelationshipStatus < EnumerateIt::Base associate_values married: 1, single: 2 - sort_by :name + sort_by :translation end ``` The `sort_by` methods accept one of the following values: -| Value | Behavior | -| :------------- | :------------------------------------------------------------------------- | -| `:translation` | The default behavior, will sort the returned values based on translations | -| `:name` | Will sort the returned values based on the name of each enumeration option | -| `:none` | Will return values in order that was passed to `associate_values` call | +| Value | Behavior | +| :------------- | :------------------------------------------------------------------------------------------- | +| `:none` | The default behavior, will return values in order that was passed to `associate_values` call | +| `:translation` | will sort the returned values based on translations | +| `:name` | Will sort the returned values based on the name of each enumeration option | ## Using enumerations diff --git a/lib/enumerate_it/base.rb b/lib/enumerate_it/base.rb index f53f0a2..612dcca 100644 --- a/lib/enumerate_it/base.rb +++ b/lib/enumerate_it/base.rb @@ -93,7 +93,7 @@ def translate(value) private def sorted_map - return enumeration if sort_mode == :none + return enumeration if sort_mode.nil? || sort_mode == :none enumeration.sort_by { |k, v| sort_lambda.call(k, v) } end @@ -103,7 +103,7 @@ def sort_lambda value: ->(_k, v) { v[0] }, name: ->(k, _v) { k }, translation: ->(_k, v) { translate(v[1]) } - }[sort_mode || :translation] + }[sort_mode] end def normalize_enumeration(values_hash) diff --git a/spec/enumerate_it/base_spec.rb b/spec/enumerate_it/base_spec.rb index 00154fd..4ed8cc3 100644 --- a/spec/enumerate_it/base_spec.rb +++ b/spec/enumerate_it/base_spec.rb @@ -207,6 +207,14 @@ end end + context 'not specifying a sort mode' do + subject { create_enumeration_class_with_sort_mode(nil).to_a } + + it 'does not sort' do + is_expected.to eq([%w(xyz 1), %w(fgh 2), %w(abc 3), %w(jkl 0)]) + end + end + context 'specifying a sort mode' do subject { create_enumeration_class_with_sort_mode(sort_mode).to_a }