assoc_whisperer 2.0.3 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/Gemfile +2 -0
- data/README.md +213 -8
- data/app/helpers/action_view/helpers/assoc_whisperer_helper.rb +98 -0
- data/app/helpers/action_view/helpers/tags/assoc_whisperer_field.rb +1 -1
- data/assoc_whisperer.gemspec +3 -3
- data/lib/assoc_whisperer/template.rb +69 -11
- data/lib/assoc_whisperer/version.rb +1 -1
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 809af461432f6d16ecf7c37cebefa320a0357a69
|
4
|
+
data.tar.gz: e938b814f1fe952c697c9118f2ccd7cc1069f6ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a297dd703d02ba14ec9a31494fa42a9d3c454b663d5076e3fec44f0955b54de902abe598a3cd9869b0c17bcc09d7a145d86e691b1fac503ec15bfa3c7759eb80
|
7
|
+
data.tar.gz: 43766aeabec37ae636a8248aaff0bb2a8da670ae82b87a29247abde90887b5c1b70259602288e6a223c71d5c680f7a324eab1d94a945e61de52c8f1aa98d68ae
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,24 +1,229 @@
|
|
1
1
|
# AssocWhisperer
|
2
2
|
|
3
|
-
|
3
|
+
This library served me to bring together some code that was scatered around multiple projects build upon Ruby on Rails.
|
4
|
+
Many times I needed a way to let user enter an associated model into form, but users don't know the exact IDs,
|
5
|
+
they know a title or some part of name, etc. But a server needs exact ID. There comes whisperer, common input field,
|
6
|
+
using AJAX to show user a narrowed list of desired records.
|
7
|
+
This is my version build upon Rails form helpers syntax, encapsulating everything needed but beeing smallest possible.
|
4
8
|
|
5
9
|
## Installation
|
6
10
|
|
7
11
|
Add this line to your application's Gemfile:
|
8
12
|
|
9
|
-
gem 'assoc_whisperer'
|
13
|
+
gem 'assoc_whisperer', '~> 2.1.0'
|
14
|
+
|
15
|
+
## Documentation
|
10
16
|
|
11
|
-
|
17
|
+
Start up yard server and browse the documentation on local:
|
12
18
|
|
13
|
-
|
19
|
+
bundle exec yard doc && bundle exec yard server
|
14
20
|
|
15
|
-
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Let's say you have a bussines where to each project can be assigned only on of your many employees. Having a form
|
24
|
+
of a project you'd have to know IDs of those people id database to fill in. Using this whisperer:
|
25
|
+
|
26
|
+
```
|
27
|
+
<%= form_for @project do |f| %>
|
28
|
+
assigned employee: <%= f.whisperer :employee, whisperer_template('get_employees') %>
|
29
|
+
<% end %>
|
30
|
+
```
|
31
|
+
|
32
|
+
It will render the form with whisperer div that includes a hidden field for desired value and a text field for user input.
|
33
|
+
(And optionaly a drop down button).
|
34
|
+
|
35
|
+
For more generice use (i. e. without form builder), there's another helper:
|
36
|
+
|
37
|
+
```
|
38
|
+
<%= form_tag create_project_path %>
|
39
|
+
assigned employee: <%= whisperer_tag :employee_id, whisperer_template('get_employees').from_params(params, :employee_id) %>
|
40
|
+
<% end %>
|
41
|
+
```
|
42
|
+
|
43
|
+
There's quite a lot to adjust on the background so let's go step by step.
|
44
|
+
|
45
|
+
## Basic case background
|
46
|
+
|
47
|
+
There's two things needed to set up - the path for that AJAX querry:
|
48
|
+
|
49
|
+
```rb
|
50
|
+
# set up a controller
|
51
|
+
class WhispererController < ApplicationController
|
52
|
+
def get_employees
|
53
|
+
user_input = params[:input]
|
54
|
+
return render(nothing: true, status: :bad_request) unless user_input
|
55
|
+
@objects = Employee.limit 10
|
56
|
+
@objects = @objects.where Employee.arel_table[:surname].matches("%#{user_input}%") unless user_input.empty?
|
57
|
+
render 'assoc_whisperer/list', layout: false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
# add to routes.rb
|
61
|
+
get 'whisp/:action', to: 'whisperer'
|
62
|
+
```
|
63
|
+
|
64
|
+
And a way to include assets (js and css) on the page
|
65
|
+
|
66
|
+
```
|
67
|
+
# add to application.js
|
68
|
+
//= require assoc_whisp
|
69
|
+
# add to application.css
|
70
|
+
*= require assoc_whisp_example
|
71
|
+
```
|
72
|
+
|
73
|
+
Again, the most simple whisperer tag:
|
74
|
+
|
75
|
+
```
|
76
|
+
<%= form_for @project do |f| %>
|
77
|
+
assigned employee: <%= f.whisperer :employee, whisperer_template('get_employees') %>
|
78
|
+
<% end %>
|
79
|
+
```
|
80
|
+
|
81
|
+
User fills in 'mori', waits for options to pop up, selects from list the desired person and submits the form:
|
82
|
+
|
83
|
+
```rb
|
84
|
+
# you'll receive these params
|
85
|
+
{'employee_id' => '5', 'employee_txt' => 'Dean Moriarty'}
|
86
|
+
# given this record exists:
|
87
|
+
Employee.find(5).to_s
|
88
|
+
# => "Dean Moriarty"
|
89
|
+
```
|
90
|
+
|
91
|
+
### Changing default settings
|
92
|
+
|
93
|
+
In the basic case, we were stuck with default options. First there's that you get ID behind #id of the object
|
94
|
+
and user sees string that returns #to_s method.
|
95
|
+
|
96
|
+
```rb
|
97
|
+
AssocWhisperer.def_value = :my_id # def: :id
|
98
|
+
AssocWhisperer.def_text = :to_my_string # def: :to_s
|
99
|
+
```
|
100
|
+
|
101
|
+
You can modify the path for AJAX request. reflect the change also in routes.rb
|
102
|
+
|
103
|
+
```rb
|
104
|
+
AssocWhisperer.def_url = '/whisperer/path' # def: '/whisp'
|
105
|
+
```
|
106
|
+
|
107
|
+
If you don't want to attach whisperers assets for every page (i.e. in application.js), there's option to define a method
|
108
|
+
that whisperer helpers call internally so it'll be attached only whether whisperer is present on the page.
|
109
|
+
|
110
|
+
```rb
|
111
|
+
# in application_helper.rb
|
112
|
+
def attach_whisperer_assets
|
113
|
+
content_for :addon_assets, javascript_include_tag('assoc_whisp')
|
114
|
+
content_for :addon_assets, stylesheet_link_tag('assoc_whisp_example')
|
115
|
+
end
|
116
|
+
# to change the helper method
|
117
|
+
AssocWhisperer.attach_assets = :custom_whisperer_attach_method
|
118
|
+
```
|
119
|
+
|
120
|
+
To make this work, you need insert that content into your layout file, within <head> preferably:
|
121
|
+
|
122
|
+
```
|
123
|
+
<%= content_for :addon_assets %>
|
124
|
+
```
|
125
|
+
|
126
|
+
### Templates
|
127
|
+
|
128
|
+
If you wanth to whisper the same fing on multiple forms, you can save somewhere the template. The template is there
|
129
|
+
even for change things around.
|
130
|
+
|
131
|
+
```rb
|
132
|
+
whisperer_template('action') # is just wrapper for AssocWhisperer::Template constructor
|
133
|
+
# you can change methods of text and value
|
134
|
+
f.whisperer :employee ,whisperer_template('get_employees', text: :full_name, value: :personal_key)
|
135
|
+
# you can show up the drop down button
|
136
|
+
f.whisperer :employee ,whisperer_template('get_employees', button: true)
|
137
|
+
# or define exact path
|
138
|
+
f.whisperer :employee ,whisperer_template('/whisperer/get_employees')
|
139
|
+
```
|
140
|
+
|
141
|
+
### Another way to set up path for AJAX:
|
142
|
+
|
143
|
+
There a way to send custom params with that particular whisperer. That's usefull if you want to completly
|
144
|
+
change controller side of AJAX calls.
|
145
|
+
|
146
|
+
```rb
|
147
|
+
f.whisperer :employee ,whisperer_template('/whisperer').params('get' => 'employees')
|
148
|
+
# routes.rb
|
149
|
+
get 'whisperer', to: 'whisperer#whisper'
|
150
|
+
```
|
151
|
+
|
152
|
+
## More complex examples
|
153
|
+
|
154
|
+
For sequent examples we're going to use this controller set up:
|
155
|
+
|
156
|
+
```rb
|
157
|
+
class WhispererController < ApplicationController
|
158
|
+
layout false
|
159
|
+
before_filter :get_input
|
160
|
+
|
161
|
+
def get_employees
|
162
|
+
@objects = Employee.limit 10
|
163
|
+
@objects = @objects.where deparatment: params[:department] if params[:department].present?
|
164
|
+
@objects = @objects.where Employee.arel_table[:surname].matches("%#{user_input}%") unless user_input.empty?
|
165
|
+
render 'assoc_whisperer/list', locales: {text: :full_name} # THIS IS IMPORTANT to show right text in list
|
166
|
+
end
|
167
|
+
|
168
|
+
private
|
169
|
+
|
170
|
+
def get_input
|
171
|
+
@input = params[:input]
|
172
|
+
render nothing: true, status: :bad_request unless @input
|
173
|
+
end
|
174
|
+
|
175
|
+
end
|
176
|
+
```
|
177
|
+
|
178
|
+
### Using the most convenient rails form building syntax
|
179
|
+
|
180
|
+
If it's the case that can use form builder for given object, it's easy to make things work. This way values will be filled
|
181
|
+
even if you return the form to user because some validation errors, for example. Since params will include following,
|
182
|
+
you can simply create object of Project and input field will fill automaticaly with what employee user selected:
|
183
|
+
|
184
|
+
```rb
|
185
|
+
# params include:
|
186
|
+
{"project" => {"employee_id" => 5, "employee_txt" => "Dean Moriarty"}}
|
187
|
+
|
188
|
+
# in controller's particular action
|
189
|
+
@project = Project.new params.require(:project).permit(:employee_id)
|
190
|
+
```
|
191
|
+
|
192
|
+
And whisperer tag only needs this:
|
193
|
+
|
194
|
+
```
|
195
|
+
<%= form_for @project do |f| %>
|
196
|
+
assigned employee: <%= f.whisperer :employee, whisperer_template('get_employees', text: :full_name).
|
197
|
+
params(department: 'management') %>
|
198
|
+
<% end %>
|
199
|
+
```
|
200
|
+
|
201
|
+
### Use without form builder
|
202
|
+
|
203
|
+
Since whisperer tag is not bound to a project that holds association to selected employee, you have to fill in values
|
204
|
+
of that employee back manually. If you have the employee object or it's nil:
|
205
|
+
|
206
|
+
```rb
|
207
|
+
whisperer_tag :employee_id, whisperer_template('get_employees', text: :full_name).
|
208
|
+
params(department: 'management').for_object(@selected_employee)
|
209
|
+
```
|
210
|
+
|
211
|
+
Or set particular values:
|
212
|
+
|
213
|
+
```rb
|
214
|
+
whisperer_tag :employee_id, whisperer_template('get_employees', text: :full_name).
|
215
|
+
params(department: 'management').value_text(params[:employee_id], params[:employee_id_text])
|
216
|
+
```
|
217
|
+
|
218
|
+
There's this short cut for when those values are in some hash, that paramas exactly is:
|
219
|
+
|
220
|
+
```rb
|
221
|
+
whisperer_tag :employee_id, whisperer_template('get_employees', text: :full_name).
|
222
|
+
params(department: 'management').from_params(params, :employee_id)
|
223
|
+
```
|
16
224
|
|
17
|
-
$ gem install assoc_whisperer
|
18
225
|
|
19
|
-
## Usage
|
20
226
|
|
21
|
-
TODO: Write usage instructions here
|
22
227
|
|
23
228
|
## Contributing
|
24
229
|
|
@@ -3,6 +3,43 @@ module ActionView
|
|
3
3
|
|
4
4
|
module AssocWhispererHelper
|
5
5
|
|
6
|
+
# This gets called for the most common case of using {FormBuilder#whisperer}, but directly useful for those situations
|
7
|
+
# when you have a custom class that has a model as an attribute and you cannot use +form_for+ for this class.
|
8
|
+
#
|
9
|
+
# # Borrow helper class:
|
10
|
+
# class Borrow
|
11
|
+
# attr_accessor :book_id, :client
|
12
|
+
# def book
|
13
|
+
# @book ||= Book.find_by(id: book_id)
|
14
|
+
# end
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # Book model:
|
18
|
+
# class Book < ActiveRecord::Base
|
19
|
+
# validates_presence_of :title
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# # in controller
|
23
|
+
# def request_borrow
|
24
|
+
# @borrow = Borrow.new
|
25
|
+
# @borrow.client = Client.find_by id: params[:client_id]
|
26
|
+
# @borrow.book_id = params[:book_id]
|
27
|
+
# # do what ever you want with the @borrow
|
28
|
+
# end
|
29
|
+
#
|
30
|
+
# # the form
|
31
|
+
# <%= form_tag request_borrow_path do %>
|
32
|
+
# <%= hidden_field_tag :client_id, @borrow.client.id %>
|
33
|
+
# book: <%= whisperer :borrow, :book, whisperer_template('get_books', text: :title) %>
|
34
|
+
# <% end %>
|
35
|
+
#
|
36
|
+
# #params will look like this: {"borrow" => {"client_id" => 1, "book_id" => 5, "book_title" => "On the road"}}
|
37
|
+
#
|
38
|
+
# @param [Symbol, String] object_name name of variable that holds the object
|
39
|
+
# @param [Symbol, String] method name of method to get desired model
|
40
|
+
# @param [AssocWhisperer::Template] template whisperer template that specifies resulting tags.
|
41
|
+
# @param [Hash] field_attrs additional attributes for text field input.
|
42
|
+
# @return [String] a html content of asscociation whisperer
|
6
43
|
def whisperer(object_name, method, template, field_attrs={})
|
7
44
|
raise "Helper '#whisperer' cannot be used in Rails < 4.x. Use '#whisperer_tag' instead." if Rails::VERSION::STRING.to_i < 4
|
8
45
|
wrapper_whisperer_assets
|
@@ -10,6 +47,32 @@ module ActionView
|
|
10
47
|
Tags::AssocWhispererField.new(object_name, method, self, template, field_attrs).render
|
11
48
|
end
|
12
49
|
|
50
|
+
# Form helper for generic associaction whisperer tag. It includes a hidden value field, a text field for user input
|
51
|
+
# and optionaly a dropdown button. The list with options that server returns is appended into this tag.
|
52
|
+
#
|
53
|
+
# This generic version is suitable for most situations. For Rails 3 there's no other option anyway.
|
54
|
+
# Arguments sets html name of the value field (the text field has suffix +_txt+ by default),
|
55
|
+
# {AssocWhisperer::Template a whisperer template}
|
56
|
+
# and additional custom attributes for the text field. Thus the latter example overrides text field
|
57
|
+
# name to +book_title+ instead +book_id_txt+ as in the former.
|
58
|
+
#
|
59
|
+
# my_book_template = AssocWhisperer::Template.new 'get_books'
|
60
|
+
# whisperer_tag :book_id, my_book_template
|
61
|
+
# whisperer_tag :book_id, my_book_template, name: :book_title, placeholder: "book's title outset"
|
62
|
+
#
|
63
|
+
# If you want to send additional parametrs with the request please refer to {AssocWhisperer::Template#params}.
|
64
|
+
# If you want to preset some value, e.g. in the case you're returning a form back to user, use either
|
65
|
+
# {AssocWhisperer::Template#from_params} or {AssocWhisperer::Template#value_text} or {AssocWhisperer::Template#for_object}.
|
66
|
+
# The last case depends on what text and value methods are set for given {AssocWhisperer::Template template}.
|
67
|
+
#
|
68
|
+
# whisperer_tag :book_id, whisperer_tempate('get_books').from_params(params, 'book_id') # params or any Hash
|
69
|
+
# whisperer_tag :book_id, whisperer_tempate('get_books').value_text(@book.id, @book.to_s)
|
70
|
+
# whisperer_tag :book_id, whisperer_tempate('get_books').for_object(@book)
|
71
|
+
#
|
72
|
+
# @param [String, Symbol] name base name for the html form inputs.
|
73
|
+
# @param [AssocWhisperer::Template] template whisperer template that specifies resulting tags.
|
74
|
+
# @param [Hash] field_attrs additional attributes for text field input.
|
75
|
+
# @return [String] a html content of asscociation whisperer
|
13
76
|
def whisperer_tag(name, template, field_attrs={})
|
14
77
|
wrapper_whisperer_assets
|
15
78
|
|
@@ -17,12 +80,16 @@ module ActionView
|
|
17
80
|
'data-opts' => template.whisperer_settings.to_json
|
18
81
|
end
|
19
82
|
|
83
|
+
# Wrapper for {AssocWhisperer::Template} constructor
|
84
|
+
# @return [AssocWhisperer::Template]
|
20
85
|
def whisperer_template(action, opts={})
|
21
86
|
AssocWhisperer::Template.new action, opts
|
22
87
|
end
|
23
88
|
|
24
89
|
private
|
25
90
|
|
91
|
+
# This is wraper for attaching assets (calls a method specified by {AssocWhisperer.attach_assets} - default
|
92
|
+
# is +attach_whisperer_assets+). Gets be called by every whisperer helper but only once assets are attached.
|
26
93
|
def wrapper_whisperer_assets
|
27
94
|
unless @assoc_whisp_attached
|
28
95
|
attach_method = AssocWhisperer.attach_assets
|
@@ -34,6 +101,37 @@ module ActionView
|
|
34
101
|
end
|
35
102
|
|
36
103
|
class FormBuilder
|
104
|
+
# Wraps {AssocWhispererHelper#whisperer} for defaul +FormBuilder+. This enables standard Rails
|
105
|
+
# form building syntax for nested models.
|
106
|
+
#
|
107
|
+
# # Borrow model:
|
108
|
+
# class Borrow < ActiveRecord::Base
|
109
|
+
# belongs_to :client
|
110
|
+
# belongs_to :book
|
111
|
+
# validates_presence_of :client, :book
|
112
|
+
# end
|
113
|
+
#
|
114
|
+
# # Book model:
|
115
|
+
# class Book < ActiveRecord::Base
|
116
|
+
# has_many :borrow
|
117
|
+
# validates_presence_of :title
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# # in controller
|
121
|
+
# @borrow = Borrow.new params.require(:borrow).permit(%i(client_id, book_id))
|
122
|
+
#
|
123
|
+
# # A form for request new borrow in library
|
124
|
+
# <%= form_for @borrow do |f| %>
|
125
|
+
# <%= f.hidden_field :client_id %>
|
126
|
+
# book: <%= f.whisperer :book, whisperer_template('get_books', text: :title) %>
|
127
|
+
# <% end %>
|
128
|
+
#
|
129
|
+
# #params will look like this: {"borrow" => {"client_id" => 1, "book_id" => 5, "book_title" => "On the road"}}
|
130
|
+
#
|
131
|
+
# @param [Symbol, String] method name of form object's nested object
|
132
|
+
# @param [AssocWhisperer::Template] template whisperer template that specifies resulting tags.
|
133
|
+
# @param [Hash] field_attrs additional attributes for text field input.
|
134
|
+
# @return [String] a html content of asscociation whisperer
|
37
135
|
def whisperer(method, template, field_attrs={})
|
38
136
|
@template.whisperer @object_name, method, template, objectify_options(field_attrs)
|
39
137
|
end
|
@@ -9,7 +9,7 @@ module ActionView
|
|
9
9
|
end
|
10
10
|
|
11
11
|
def render
|
12
|
-
@whisp_template.
|
12
|
+
@whisp_template.for_object whispered_object
|
13
13
|
|
14
14
|
contents = @whisp_template.value_field_tag value_tag_id, value_tag_name
|
15
15
|
contents += @whisp_template.text_field_tag text_tag_id, text_tag_name, @options
|
data/assoc_whisperer.gemspec
CHANGED
@@ -8,9 +8,9 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.version = AssocWhisperer::VERSION
|
9
9
|
spec.authors = ["Ondřej Želazko"]
|
10
10
|
spec.email = ["zelazk.o@email.cz"]
|
11
|
-
spec.summary = %q{Rails tag
|
12
|
-
spec.description = %q{
|
13
|
-
spec.homepage = ""
|
11
|
+
spec.summary = %q{Rails whisperer tag for forms}
|
12
|
+
spec.description = %q{Input associated models directly by id}
|
13
|
+
spec.homepage = "https://github.com/doooby/assoc_whisperer"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -1,36 +1,85 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
|
3
3
|
module AssocWhisperer
|
4
|
+
|
5
|
+
# This object specifies what will a whisperer tag look like. {ActionView::Helpers::AssocWhispererHelper#whisperer_template}
|
6
|
+
# is a markup for this class.
|
4
7
|
class Template
|
5
8
|
attr_reader :opts
|
6
9
|
|
10
|
+
# Creates a template for whisperer.
|
11
|
+
# @param [String] action can be just a tail of url ({AssocWhisperer.def_url}
|
12
|
+
# will be prefix) or full path eg.: +'/whisperer/specific_path'+.
|
13
|
+
# Options may include:
|
14
|
+
# +:value+:: a method name that is called on given object to get the value the hidden field will be filled with.
|
15
|
+
# If not specified then {AssocWhisperer.def_value} is used.
|
16
|
+
# +:text+:: a method name that is called on given object to get the value for the text field.
|
17
|
+
# If not specified then {AssocWhisperer.def_text} is used.
|
18
|
+
# +:button+:: specifies whether dropdown button will be present. Defaults to false.
|
7
19
|
def initialize(action, opts={})
|
8
20
|
@action = action[0]=='/' ? action : "#{AssocWhisperer.def_url}/#{action}"
|
9
21
|
@opts = opts
|
10
22
|
@opts[:value] = AssocWhisperer.def_value unless @opts[:value]
|
11
23
|
@opts[:text] = AssocWhisperer.def_text unless @opts[:text]
|
12
|
-
@opts[:button] =
|
24
|
+
@opts[:button] = false if @opts[:button].nil?
|
13
25
|
end
|
14
26
|
|
27
|
+
# Sets parameters that will be included to html request. Usefull to specify additional options. Because this returns
|
28
|
+
# the template object itself, it is easy to use in views.
|
29
|
+
# whisperer_tag :foo, whisperer_template('action').params(bar: 5)
|
30
|
+
# Whisperer will call something like this url +/whisp/action?bar=5+
|
31
|
+
# @param [Hash] params
|
32
|
+
# @return [Template]
|
15
33
|
def params(params)
|
16
34
|
@params = params
|
17
35
|
self
|
18
36
|
end
|
19
37
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
@value =
|
29
|
-
@text =
|
38
|
+
# Sets value and text for given object and returns tepmlate object itself for ease of use.
|
39
|
+
# whisperer_tag :foo, whisperer_template('action', text: :to_readable_text).value_text(@foo_object)
|
40
|
+
# # value => FooObject#id (since :id is default method for value specified in AssocWhisperer.def_value)
|
41
|
+
# # text => FooObject#to_readable_text
|
42
|
+
# @param [Object] object from which will be the value and text taken.
|
43
|
+
# @return [Template]
|
44
|
+
def for_object(object)
|
45
|
+
if object
|
46
|
+
@value = (object.send @opts[:value] if object.respond_to? @opts[:value])
|
47
|
+
@text = (object.send @opts[:text] if object.respond_to? @opts[:text])
|
30
48
|
end
|
31
49
|
self
|
32
50
|
end
|
33
51
|
|
52
|
+
# Sets value and text that is specified in given hash (like params) and returns tepmlate object itself.
|
53
|
+
# In case text is under something else then value name with suffix +_txt+, specify it in last argument.
|
54
|
+
# params = {'foo' => '15', 'foo_txt' => 'Foo number 15'}
|
55
|
+
# whisperer_tag :foo, whisperer_template('action').from_params(params, 'foo')
|
56
|
+
# params = {'foo' => '15', 'foo_bar' => 'Foo number 15'}
|
57
|
+
# whisperer_tag :foo, whisperer_template('action').from_params(params, 'foo', 'foo_bar')
|
58
|
+
# @param [Hash] hash containing values (like params)
|
59
|
+
# @param [String] value_name the key under which is the value in the hash
|
60
|
+
# @param [String] text_name the key under which is the text in the hash
|
61
|
+
# @return [Template]
|
62
|
+
def from_params(hash, value_name, text_name=nil)
|
63
|
+
@value = hash[value_name]
|
64
|
+
@text = hash[text_name || "#{value_name}_txt"]
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
68
|
+
# Sets value and text and returns tepmlate object itself.
|
69
|
+
# whisperer_tag :foo, whisperer_template('action').value_text(foo.id, foo.to_s)
|
70
|
+
# @param [Object] value that will converted to string and filled into hidden input field
|
71
|
+
# @param [String] text that will be filled into text field
|
72
|
+
# @return [Template]
|
73
|
+
def value_text(value, text)
|
74
|
+
@value = value
|
75
|
+
@text = text
|
76
|
+
self
|
77
|
+
end
|
78
|
+
|
79
|
+
# Creates html contents for whisperer (used by {ActionView::Helpers::AssocWhispererHelper#whisperer_tag})
|
80
|
+
# @param [String] input_name for html form name of value and text field tags
|
81
|
+
# @param [Hash] field_attrs additional attributes for text field tag
|
82
|
+
# @return [String] tag html content
|
34
83
|
def tag_contents(input_name, field_attrs={})
|
35
84
|
input_name = input_name.to_s
|
36
85
|
sanitized_id = input_name.dup.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_")
|
@@ -42,6 +91,8 @@ module AssocWhisperer
|
|
42
91
|
contents + dropdown_button_tag
|
43
92
|
end
|
44
93
|
|
94
|
+
# This is used for whisperer's +data-opts+ html attribute
|
95
|
+
# @return [Hash] whisperer's settings
|
45
96
|
def whisperer_settings
|
46
97
|
h = {action: @action}
|
47
98
|
# h[:cs] = @opts[:client_side] if @opts[:client_side]
|
@@ -49,10 +100,14 @@ module AssocWhisperer
|
|
49
100
|
h
|
50
101
|
end
|
51
102
|
|
103
|
+
# Creates html contents for hidden field that holds a value
|
104
|
+
# @return [String] hidden field html
|
52
105
|
def value_field_tag(id, name)
|
53
106
|
%(<input type="hidden" id="#{id}" name="#{name}" value="#{@value}" class="value_field">)
|
54
107
|
end
|
55
108
|
|
109
|
+
# Creates html contents for text field
|
110
|
+
# @return [String] text field html
|
56
111
|
def text_field_tag(id, name, attrs={})
|
57
112
|
attrs[:size] = 12 unless attrs.has_key? :size
|
58
113
|
keys_whitelist = (attrs.keys & [:size, :placeholder, :maxlength, :title])
|
@@ -63,6 +118,9 @@ module AssocWhisperer
|
|
63
118
|
%(<input type="text" autocomplete="off" id="#{id}" name="#{name}" value="#{@text}" class="text_field#{' unfilled' unless @value}"#{attrs * ' '}>)
|
64
119
|
end
|
65
120
|
|
121
|
+
# Creates html contents for drop down button. If +:button+ option is anything else then TrueClass,
|
122
|
+
# empty string is returned.
|
123
|
+
# @return [String] span html
|
66
124
|
def dropdown_button_tag
|
67
125
|
return '' unless @opts[:button].is_a? TrueClass
|
68
126
|
"<span class=\"dropdown_button querying\">\u25BE</span>"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: assoc_whisperer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ondřej Želazko
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -38,8 +38,7 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
42
|
-
server recieves id
|
41
|
+
description: Input associated models directly by id
|
43
42
|
email:
|
44
43
|
- zelazk.o@email.cz
|
45
44
|
executables: []
|
@@ -62,7 +61,7 @@ files:
|
|
62
61
|
- lib/assoc_whisperer.rb
|
63
62
|
- lib/assoc_whisperer/template.rb
|
64
63
|
- lib/assoc_whisperer/version.rb
|
65
|
-
homepage:
|
64
|
+
homepage: https://github.com/doooby/assoc_whisperer
|
66
65
|
licenses:
|
67
66
|
- MIT
|
68
67
|
metadata: {}
|
@@ -82,8 +81,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
82
81
|
version: '0'
|
83
82
|
requirements: []
|
84
83
|
rubyforge_project:
|
85
|
-
rubygems_version: 2.4.
|
84
|
+
rubygems_version: 2.4.5
|
86
85
|
signing_key:
|
87
86
|
specification_version: 4
|
88
|
-
summary: Rails tag
|
87
|
+
summary: Rails whisperer tag for forms
|
89
88
|
test_files: []
|
89
|
+
has_rdoc:
|