x-editable-rails-bs4 1.5.5.2
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 +7 -0
- data/LICENSE.txt +22 -0
- data/README.md +221 -0
- data/lib/generators/x_editable_rails/install_generator.rb +12 -0
- data/lib/generators/x_editable_rails/templates/x-editable.yml +19 -0
- data/lib/x-editable-rails.rb +15 -0
- data/lib/x-editable-rails/configuration.rb +62 -0
- data/lib/x-editable-rails/version.rb +7 -0
- data/lib/x-editable-rails/view_helpers.rb +167 -0
- data/vendor/assets/images/editable/clear.png +0 -0
- data/vendor/assets/images/editable/loading.gif +0 -0
- data/vendor/assets/javascripts/editable/bootstrap-editable.js +6722 -0
- data/vendor/assets/javascripts/editable/bootstrap2-editable.js +7007 -0
- data/vendor/assets/javascripts/editable/bootstrap3-editable.js +6807 -0
- data/vendor/assets/javascripts/editable/inputs-ext/address.js +170 -0
- data/vendor/assets/javascripts/editable/inputs-ext/bootstrap-wysihtml5.js +499 -0
- data/vendor/assets/javascripts/editable/inputs-ext/typeahead-editable.js +86 -0
- data/vendor/assets/javascripts/editable/inputs-ext/typeahead.js +1139 -0
- data/vendor/assets/javascripts/editable/inputs-ext/wysihtml5-editable.js +146 -0
- data/vendor/assets/javascripts/editable/inputs-ext/wysihtml5.js +9521 -0
- data/vendor/assets/javascripts/editable/jquery-editable-poshytip.js +5118 -0
- data/vendor/assets/javascripts/editable/jqueryui-editable.js +5065 -0
- data/vendor/assets/javascripts/editable/rails.js.coffee +3 -0
- data/vendor/assets/javascripts/editable/rails/data_classes.js.coffee +23 -0
- data/vendor/assets/javascripts/editable/rails/editable_form.js.coffee +51 -0
- data/vendor/assets/javascripts/editable/rails/error_handling.js.coffee +4 -0
- data/vendor/assets/stylesheets/editable/bootstrap-editable.scss +674 -0
- data/vendor/assets/stylesheets/editable/bootstrap2-editable.scss +655 -0
- data/vendor/assets/stylesheets/editable/bootstrap3-editable.scss +655 -0
- data/vendor/assets/stylesheets/editable/inputs-ext/address.css +9 -0
- data/vendor/assets/stylesheets/editable/inputs-ext/bootstrap-typeahead.css +49 -0
- data/vendor/assets/stylesheets/editable/inputs-ext/bootstrap-wysihtml5.css +102 -0
- data/vendor/assets/stylesheets/editable/inputs-ext/wysiwyg-color.css +67 -0
- data/vendor/assets/stylesheets/editable/jquery-editable.scss +205 -0
- data/vendor/assets/stylesheets/editable/jqueryui-editable.scss +205 -0
- metadata +121 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7b318db8e6922984c3d7e2fca3438daa69a11fe6e85e43d471c7122d70b46999
|
4
|
+
data.tar.gz: 6811a0a6d5ad9f7e28a98ff9657d5a9f1bb96fd6eae7c868fc9ab82cf6197c93
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36b4a011c954489a18817aafaca557a590a43c07c56a07d061f3e6600ea7665e82a4f4bcc71fabf4c3a2175ff7acf93f05e980a166e8aedc60a4624cdd8c6351
|
7
|
+
data.tar.gz: 4e194461803bb0550dfc4ee39f7b9fb352aeda8cb67390a6a9fa11922f73721714b2d0037f9e5794bc7a1c28c19de75b5f58ee51baa26fcd1aa7fb61d349ca81
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jiri Kolarik
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,221 @@
|
|
1
|
+
# X::Editable::Rails
|
2
|
+
|
3
|
+
[](https://travis-ci.org/werein/x-editable-rails) [](https://codeclimate.com/github/werein/x-editable-rails) [](https://codeclimate.com/github/werein/x-editable-rails) [](https://badge.fury.io/rb/x-editable-rails) [](https://gemnasium.com/werein/x-editable-rails)
|
4
|
+
|
5
|
+
X-editable for Rails
|
6
|
+
|
7
|
+
## Live demo
|
8
|
+
|
9
|
+
Checkout live demo [here](https://x-editable-rails.herokuapp.com/?denied=true)
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
gem 'x-editable-rails'
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install x-editable-rails
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Assets
|
28
|
+
|
29
|
+
Choose between the following javascripts:
|
30
|
+
|
31
|
+
* bootstrap-editable
|
32
|
+
* bootstrap2-editable
|
33
|
+
* jqueryui-editable
|
34
|
+
* jquery-editable-poshytip
|
35
|
+
|
36
|
+
You'll also need to include editable/rails in your scripts for this to work.
|
37
|
+
|
38
|
+
```coffee
|
39
|
+
#= require editable/bootstrap-editable
|
40
|
+
#= require editable/rails
|
41
|
+
```
|
42
|
+
|
43
|
+
Choose the corresponding stylesheets:
|
44
|
+
|
45
|
+
* bootstrap-editable
|
46
|
+
* bootstrap2-editable
|
47
|
+
* jqueryui-editable
|
48
|
+
* jquery-editable
|
49
|
+
|
50
|
+
```scss
|
51
|
+
// as CSS
|
52
|
+
*= require editable/bootstrap-editable
|
53
|
+
|
54
|
+
// or SCSS
|
55
|
+
@import "editable/bootstrap-editable";
|
56
|
+
```
|
57
|
+
|
58
|
+
Enable editing with jQuery:
|
59
|
+
|
60
|
+
```coffee
|
61
|
+
$('.editable').editable()
|
62
|
+
```
|
63
|
+
|
64
|
+
For custom inputs like the Wysihtml5 editor, add these dependencies:
|
65
|
+
|
66
|
+
```coffee
|
67
|
+
#= require editable/bootstrap-editable
|
68
|
+
#= require editable/inputs-ext/wysihtml5
|
69
|
+
#= require editable/inputs-ext/bootstrap-wysihtml5
|
70
|
+
#= require editable/inputs-ext/wysihtml5-editable
|
71
|
+
#= require editable/rails
|
72
|
+
```
|
73
|
+
|
74
|
+
And related stylesheets:
|
75
|
+
|
76
|
+
```css
|
77
|
+
//= require editable/bootstrap-editable
|
78
|
+
//= require editable/inputs-ext/bootstrap-wysihtml5
|
79
|
+
//= require editable/inputs-ext/wysiwyg-color
|
80
|
+
```
|
81
|
+
|
82
|
+
### Making Things Editable
|
83
|
+
|
84
|
+
`x-editable-rails` provides a helper method in your view to make your model values editable.
|
85
|
+
By default, you need to specify the model and property that should be editable.
|
86
|
+
A `span` element is rendered with `data-*` attributes used by `x-editable`.
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
# the editable object and the attribute to edit
|
90
|
+
%h1= editable @model, :name
|
91
|
+
```
|
92
|
+
|
93
|
+
You can customize the tag name and title attribute:
|
94
|
+
|
95
|
+
* **tag** - `span` by default.
|
96
|
+
* **title** - The model and attribute name are used to create a capitalized title
|
97
|
+
|
98
|
+
The `editable` helper method automatically adds these `data-*` attributes used by [x-editable](http://vitalets.github.io/x-editable/docs.html).
|
99
|
+
|
100
|
+
* **url** - Uses the `polymorphic_path(model)` helper method.
|
101
|
+
* **source** - Only populated if the value is a boolean to convert `true` or `false` to "Yes" and "No".
|
102
|
+
* **value** - Uses `model.name`. If `model.name` were a boolean value or if a `source` is specified, the `source` text would be displayed rather than the raw value. (Presumably the value is an ID and the source would have the text associated with that value.)
|
103
|
+
* **placeholder** - Uses the `title` value by default
|
104
|
+
|
105
|
+
```ruby
|
106
|
+
# editable object, what_you_want_update, e: exception - when is xeditable? false or can? :edit, object is false
|
107
|
+
%h1= editable @model, :name, url: model_path, value: @model.name.upcase
|
108
|
+
```
|
109
|
+
|
110
|
+
Here are some special features to enhance what's baked into [x-editable](http://vitalets.github.io/x-editable/docs.html):
|
111
|
+
|
112
|
+
* **type** - The type of input is automatically detected. By default, if the value is a boolean, the `type` is "select" with a built-in `source` that outputs "Yes" and "No" (unless another `source` is specified).
|
113
|
+
* **source** - In addition to hashes or arrays of hashes, you can also use an array of strings for a simpler structure if the name and value are the same:
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
source = [ "Active", "Disabled" ]
|
117
|
+
editable @model, :enabled, source: source
|
118
|
+
```
|
119
|
+
|
120
|
+
* **value** - This option will override the `model.name` value
|
121
|
+
* **classes** - This is a custom option for `x-editable-rails` that will change the editable element's CSS class based on the selected value. Use the `source` hash structure to map a CSS class name to a value. (This [functionality](vendor/assets/javascripts/editable/rails/data_classes.js.coffee) is toggled when the value changes and the "save" event is triggered.)
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
source = [ "Active", "Disabled" ]
|
125
|
+
classes = { "Active" => "label-success", "Disabled" => "label-default" }
|
126
|
+
editable @model, :enabled, source: source, classes: classes, class: "label"
|
127
|
+
```
|
128
|
+
|
129
|
+
* **nested** - Name of a nested attributes (such as [globalize](https://github.com/globalize/globalize)'s `translations`)
|
130
|
+
* **nid** - ID of the nested attribute
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
%h1= editable @model, :name, nested: :translations, nid: @model.translation.id
|
134
|
+
|
135
|
+
# example of nested resource
|
136
|
+
%h1= editable [picture.gallery, picture], :name, nested: :translations, nid: picture.translation.id
|
137
|
+
```
|
138
|
+
|
139
|
+
### Authorization
|
140
|
+
|
141
|
+
Add a helper method to your controllers to indicate if `x-editable` should be enabled.
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
def xeditable? object = nil
|
145
|
+
true # Or something like current_user.xeditable?
|
146
|
+
end
|
147
|
+
```
|
148
|
+
|
149
|
+
You can use [CanCan](https://github.com/ryanb/cancan) and checks the `:edit` permission for the model being edited.
|
150
|
+
|
151
|
+
```ruby
|
152
|
+
def xeditable? object = nil
|
153
|
+
can?(:edit, object) ? true : false
|
154
|
+
end
|
155
|
+
```
|
156
|
+
|
157
|
+
* **e** - Specify a custom (error) message to display if the value isn't editable
|
158
|
+
|
159
|
+
### "Don't Repeat Yourself" Templates
|
160
|
+
|
161
|
+
To make your views cleaner, you can specify all your options for each class and attribute in a YAML configuration file.
|
162
|
+
Attributes where the `title` or `placeholder` are not different except maybe capitalized can be left out because they are automatically capitalized when rendered (see above).
|
163
|
+
|
164
|
+
This example uses the `MailingList` class and its attributes.
|
165
|
+
The attribute value can be a string, which is used as the `title` and `placeholder`.
|
166
|
+
If you want to specify other options, create a hash of options.
|
167
|
+
|
168
|
+
Install configuration file like this: `rails g x_editable_rails:install`, this step is not necessary
|
169
|
+
|
170
|
+
```yaml
|
171
|
+
class_options:
|
172
|
+
MailingList:
|
173
|
+
# Specify placeholder text for each attribute or a Hash of options
|
174
|
+
name: Mailing list name
|
175
|
+
enabled:
|
176
|
+
type: select
|
177
|
+
source:
|
178
|
+
- Active
|
179
|
+
- Disabled
|
180
|
+
reply_email:
|
181
|
+
type: email
|
182
|
+
title: Reply-to email
|
183
|
+
User:
|
184
|
+
email:
|
185
|
+
type: email
|
186
|
+
password:
|
187
|
+
type: password
|
188
|
+
mailing_lists:
|
189
|
+
type: select
|
190
|
+
# specify a URL to get source via AJAX (see x-editable docs)
|
191
|
+
source: <%= ::Rails.application.routes.url_helpers.mailing_lists_source_path %>
|
192
|
+
```
|
193
|
+
|
194
|
+
Now you can specify your editable fields without options because they will be inherited from your YAML config.
|
195
|
+
|
196
|
+
```ruby
|
197
|
+
model = MailingList.new
|
198
|
+
|
199
|
+
editable model, :name # type: "text", title: "Mailing list name"
|
200
|
+
editable model, :enabled # type: "select", title: "Enabled", source: [ "Active", "Disabled" ]
|
201
|
+
```
|
202
|
+
|
203
|
+
### Examples
|
204
|
+
|
205
|
+
Gem also contains demo application with integrated x-editable
|
206
|
+
|
207
|
+
```
|
208
|
+
cd test/dummy
|
209
|
+
rake db:migrate
|
210
|
+
rake db:seed
|
211
|
+
rails g x_editable_rails:install # optional, it generate config example
|
212
|
+
rails s
|
213
|
+
```
|
214
|
+
|
215
|
+
## Contributing
|
216
|
+
|
217
|
+
1. Fork it
|
218
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
219
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
220
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
221
|
+
5. Create new Pull Request
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module XEditableRails
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
|
6
|
+
desc 'Copy example of x-editable config'
|
7
|
+
def copy_x_editable_yml
|
8
|
+
template 'x-editable.yml', 'config/x-editable.yml'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# More informations you can find here: https://github.com/werein/x-editable-rails
|
2
|
+
class_options:
|
3
|
+
MailingList:
|
4
|
+
name: Mailing list name
|
5
|
+
enabled:
|
6
|
+
type: select
|
7
|
+
source:
|
8
|
+
- Active
|
9
|
+
- Disabled
|
10
|
+
reply_email:
|
11
|
+
type: email
|
12
|
+
title: Reply-to email
|
13
|
+
User:
|
14
|
+
email:
|
15
|
+
type: email
|
16
|
+
password:
|
17
|
+
type: password
|
18
|
+
mailing_lists:
|
19
|
+
type: select
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'x-editable-rails/version'
|
2
|
+
require 'x-editable-rails/configuration'
|
3
|
+
require 'x-editable-rails/view_helpers'
|
4
|
+
|
5
|
+
module X
|
6
|
+
module Editable
|
7
|
+
module Rails
|
8
|
+
class Engine < ::Rails::Engine
|
9
|
+
initializer 'x-editable-rails.view_helpers' do
|
10
|
+
ActionView::Base.send :include, ViewHelpers
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module X
|
2
|
+
module Editable
|
3
|
+
module Rails
|
4
|
+
module Configuration
|
5
|
+
|
6
|
+
def method_options_for(object, method)
|
7
|
+
class_options_for(object).fetch(method, {})
|
8
|
+
end
|
9
|
+
|
10
|
+
def class_options_for(object)
|
11
|
+
class_options = options.fetch(:class_options, {})
|
12
|
+
class_options.fetch(object.class.name, {})
|
13
|
+
end
|
14
|
+
|
15
|
+
def class_options
|
16
|
+
options.fetch(:class_options, {})
|
17
|
+
end
|
18
|
+
|
19
|
+
def options
|
20
|
+
config_fn = ::Rails.root.join("config", "x-editable.yml")
|
21
|
+
if File.file?(config_fn)
|
22
|
+
@options ||= begin
|
23
|
+
options = load_yaml_file config_fn
|
24
|
+
format_class_options! options
|
25
|
+
end
|
26
|
+
else
|
27
|
+
@options = {}
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_yaml_file(path)
|
32
|
+
source = File.read path
|
33
|
+
source = ERB.new(source).result
|
34
|
+
|
35
|
+
result = YAML.load(source)
|
36
|
+
result = {} if result.blank?
|
37
|
+
|
38
|
+
result.with_indifferent_access
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
# if the specified options are a string, convert to Hash and make the string the title
|
44
|
+
def format_class_options!(options)
|
45
|
+
if class_options = options[:class_options]
|
46
|
+
class_options.each do |class_name, methods|
|
47
|
+
methods.each do |method, method_options|
|
48
|
+
unless method_options.is_a? Hash
|
49
|
+
methods[method] = { title: method_options }.with_indifferent_access
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
options
|
56
|
+
end
|
57
|
+
|
58
|
+
extend self
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,167 @@
|
|
1
|
+
require 'base64'
|
2
|
+
|
3
|
+
module X
|
4
|
+
module Editable
|
5
|
+
module Rails
|
6
|
+
module ViewHelpers
|
7
|
+
#
|
8
|
+
# Options determine how the HTML tag is rendered and the remaining options are converted to data-* attributes.
|
9
|
+
#
|
10
|
+
# options:
|
11
|
+
# tag: tag name of element returned
|
12
|
+
# class: "class" attribute on element
|
13
|
+
# placeholder: "placeholder" attribute on element
|
14
|
+
# title: "title" attribute on element (defaults to placeholder)
|
15
|
+
# data: "data-*" attributes on element
|
16
|
+
# source: a Hash of friendly display values used by input elements based on (object) value
|
17
|
+
# - boolean shorthand ['Enabled', 'Disabled'] becomes { '1' => 'Enabled', '0' => 'Disabled' }
|
18
|
+
# - enumerable shorthand ['Yes', 'No', 'Maybe'] becomes { 'Yes' => 'Yes', 'No' => 'No', 'Maybe' => 'Maybe' }
|
19
|
+
# classes: a Hash of classes to add based on the value (same format and shorthands as source)
|
20
|
+
# value: override the object's value
|
21
|
+
#
|
22
|
+
def editable(object, method, options = {})
|
23
|
+
options = Configuration.method_options_for(object, method).deep_merge(options).with_indifferent_access
|
24
|
+
# merge data attributes for backwards-compatibility
|
25
|
+
options.merge! options.delete(:data){ Hash.new }
|
26
|
+
|
27
|
+
url = options.delete(:url){ polymorphic_path(object) }
|
28
|
+
object = object.last if object.kind_of?(Array)
|
29
|
+
value = options.delete(:value){ object.send(method) }
|
30
|
+
source = options[:source] ? format_source(options.delete(:source), value) : default_source_for(value)
|
31
|
+
classes = format_source(options.delete(:classes), value)
|
32
|
+
error = options.delete(:e)
|
33
|
+
html_options = options.delete(:html){ Hash.new }
|
34
|
+
|
35
|
+
if xeditable?(object)
|
36
|
+
model = object.class.model_name.param_key
|
37
|
+
nid = options.delete(:nid)
|
38
|
+
nested = options.delete(:nested)
|
39
|
+
title = options.delete(:title) do
|
40
|
+
klass = nested ? object.class.const_get(nested.to_s.classify) : object.class
|
41
|
+
klass.human_attribute_name(method)
|
42
|
+
end
|
43
|
+
|
44
|
+
output_value = output_value_for(value)
|
45
|
+
css_list = options.delete(:class).to_s.split(/\s+/).unshift('editable')
|
46
|
+
css_list << classes[output_value] if classes
|
47
|
+
type = options.delete(:type){ default_type_for(value) }
|
48
|
+
css = css_list.compact.uniq.join(' ')
|
49
|
+
tag = options.delete(:tag){ 'span' }
|
50
|
+
placeholder = options.delete(:placeholder){ title }
|
51
|
+
|
52
|
+
# any remaining options become data attributes
|
53
|
+
data = {
|
54
|
+
type: type,
|
55
|
+
model: model,
|
56
|
+
name: method,
|
57
|
+
value: ( type == 'wysihtml5' ? Base64.encode64(output_value) : output_value ),
|
58
|
+
placeholder: placeholder,
|
59
|
+
classes: classes,
|
60
|
+
source: source,
|
61
|
+
url: url,
|
62
|
+
nested: nested,
|
63
|
+
nid: nid
|
64
|
+
}.merge(options.symbolize_keys)
|
65
|
+
|
66
|
+
data.reject!{|_, value| value.nil?}
|
67
|
+
|
68
|
+
html_options.update({
|
69
|
+
class: css,
|
70
|
+
title: title,
|
71
|
+
data: data
|
72
|
+
})
|
73
|
+
|
74
|
+
content_tag tag, html_options do
|
75
|
+
if %w(select checklist).include?(data[:type].to_s) && !source.is_a?(String)
|
76
|
+
source = normalize_source(source)
|
77
|
+
content = source.detect { |t| output_value == output_value_for(t[0]) }
|
78
|
+
content.present? ? content[1] : ""
|
79
|
+
else
|
80
|
+
safe_join(source_values_for(value, source), tag(:br))
|
81
|
+
end
|
82
|
+
end
|
83
|
+
else
|
84
|
+
error || safe_join(source_values_for(value, source), tag(:br))
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def normalize_source(source)
|
91
|
+
return [] unless source
|
92
|
+
source.map do |el|
|
93
|
+
if el.is_a? Array
|
94
|
+
el
|
95
|
+
else
|
96
|
+
[el[:value], el[:text]]
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def output_value_for(value)
|
102
|
+
value = case value
|
103
|
+
when TrueClass
|
104
|
+
'1'
|
105
|
+
when FalseClass
|
106
|
+
'0'
|
107
|
+
when NilClass
|
108
|
+
''
|
109
|
+
when Array
|
110
|
+
value.map{|item| output_value_for item}.join(',')
|
111
|
+
else
|
112
|
+
value.to_s
|
113
|
+
end
|
114
|
+
|
115
|
+
value
|
116
|
+
end
|
117
|
+
|
118
|
+
def source_values_for(value, source = nil)
|
119
|
+
source ||= default_source_for value
|
120
|
+
|
121
|
+
values = Array.wrap(value)
|
122
|
+
|
123
|
+
if source && ( source.first.is_a?(String) || source.kind_of?(Hash) )
|
124
|
+
values.map{|item| source[output_value_for item]}
|
125
|
+
else
|
126
|
+
values
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def default_type_for(value)
|
131
|
+
case value
|
132
|
+
when TrueClass, FalseClass
|
133
|
+
'select'
|
134
|
+
when Array
|
135
|
+
'checklist'
|
136
|
+
else
|
137
|
+
'text'
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def default_source_for(value)
|
142
|
+
case value
|
143
|
+
when TrueClass, FalseClass
|
144
|
+
{ '1' => 'Yes', '0' => 'No' }
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
# helper method that take some shorthand source definitions and reformats them
|
149
|
+
def format_source(source, value)
|
150
|
+
formatted_source = case value
|
151
|
+
when TrueClass, FalseClass
|
152
|
+
if source.is_a?(Array) && source.first.is_a?(String) && source.size == 2
|
153
|
+
{ '1' => source[0], '0' => source[1] }
|
154
|
+
end
|
155
|
+
else
|
156
|
+
if source.is_a?(Array) && source.first.is_a?(String)
|
157
|
+
source.map { |v| { value: v, text: v } }
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
formatted_source || source
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|