simple_form_extension 1.3.23 → 1.3.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ba913b639f6114ae675e258ecc87c2573301d66
4
- data.tar.gz: 79e0717e743f58193bb99e29175fa4d4607aa2a2
3
+ metadata.gz: 9846d5ea6a3dcfe7101245e6bb39173847dd8b08
4
+ data.tar.gz: ea6b4ffb914faa3a74322c75dd6f953d576c9f2d
5
5
  SHA512:
6
- metadata.gz: b2e3e1c5c4a8d020a36f21014e4c6cb03b8b52b4368cc1bdd799fd547c7ba5233f9585b0a88236d0861542d185a1f1574d942090343545fcdc3d6208e9c8ce70
7
- data.tar.gz: 0818d72b1f2987d99558ec9f16cc254a3cdf1097d220cb8a65cb223dee46af2ea64765e83fa9409b297f65fe8a6973e3266933834d9f67a19b0a3de38301b70d
6
+ metadata.gz: 0749b441d79c526913b77623ee5dce6382a21b883dd40552033aaa13091b5d6267da292f74d144bf842197c93cbc86cf9ac2b7b4897ab5294fbc37c57c29c7bf
7
+ data.tar.gz: 974bdfe2e59f31c5eaccb236debbca8f858fdeb54732b5c0eee3e0e583132c28cb93fadda2ffab1116279991f25a2a30f7c5f7b7f113e0ac1784f8acbf5005b3
data/README.md CHANGED
@@ -166,6 +166,18 @@ selectize to sort your collection.
166
166
 
167
167
  Used javascript plugin : [Selectize](https://brianreavis.github.io/selectize.js/)
168
168
 
169
+ ##### Selectize on enums
170
+
171
+ You can use a selectize input field on
172
+ [enums](http://edgeapi.rubyonrails.org/classes/ActiveRecord/Enum.html).
173
+
174
+ The different options of the enum will be displayed.
175
+ You can translate those options by defining the following translation key :
176
+ `activerecord.enums.<model_name>.<attribute_name>.<option_name>`.
177
+
178
+ For example, if we have a `User` model with an `invitation` enum and the
179
+ `accepted` state, we'll have to translate : `activerecord.enums.user.invitation.accepted`.
180
+
169
181
  #### :slider
170
182
 
171
183
  To use the slide component, you'll always want to set the `:min` and `:max`
@@ -14,6 +14,8 @@ module SimpleFormExtension
14
14
  #
15
15
  def input(wrapper_options = {})
16
16
  @attribute_name = foreign_key if relation?
17
+ set_sort_field!
18
+
17
19
  input_html_options[:data] ||= {}
18
20
 
19
21
  input_html_options[:data].merge!(
@@ -58,6 +60,14 @@ module SimpleFormExtension
58
60
  options[:max_items]
59
61
  end
60
62
 
63
+ def set_sort_field!
64
+ options[:sort_field] = if enum?
65
+ 'position'
66
+ else
67
+ 'text'
68
+ end
69
+ end
70
+
61
71
  def sort_field
62
72
  options[:sort_field] ||= 'text'
63
73
  end
@@ -73,6 +83,8 @@ module SimpleFormExtension
73
83
  end
74
84
  elsif relation?
75
85
  reflection.klass.all.map(&method(:serialize_option))
86
+ elsif enum?
87
+ enum_options
76
88
  else
77
89
  []
78
90
  end
@@ -156,6 +168,19 @@ module SimpleFormExtension
156
168
  end
157
169
  end
158
170
 
171
+ def enum?
172
+ @is_enum ||= object.class.defined_enums.key?(attribute_name.to_s)
173
+ end
174
+
175
+ def enum_options
176
+ object.class.defined_enums[attribute_name.to_s].map do |key, value|
177
+ path = [object.model_name.i18n_key, attribute_name, key].join('.')
178
+ translation = ::I18n.t("activerecord.enums.#{ path }", default: '')
179
+
180
+ { text: translation.presence || key, value: key, position: value }
181
+ end
182
+ end
183
+
159
184
  def foreign_key
160
185
  @foreign_key ||= case reflection.macro
161
186
  when :belongs_to then reflection.foreign_key
@@ -1,3 +1,3 @@
1
1
  module SimpleFormExtension
2
- VERSION = "1.3.23"
2
+ VERSION = "1.3.24"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_form_extension
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.23
4
+ version: 1.3.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glyph-fr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-06-17 00:00:00.000000000 Z
11
+ date: 2016-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails