Skip to content

Commit

Permalink
Change 'sort_by' default value from :translation to :none
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascaton committed Oct 19, 2016
1 parent 9ac0fdc commit e8bdfff
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions lib/enumerate_it/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions spec/enumerate_it/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down

0 comments on commit e8bdfff

Please sign in to comment.