assoc_whisperer 1.1.1 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b2960496ba2056b1cbdaa67fa844d589f6d99e16
4
- data.tar.gz: 974d3d0c13f0076d4a9999939b2f174179e6dfea
3
+ metadata.gz: e3b0faee0f011a70f467c0952840bf4f9769b2d7
4
+ data.tar.gz: 3c79fa3dd430cd87812df970f2587c3582fedbf0
5
5
  SHA512:
6
- metadata.gz: 8bd9f8f5160f64f80771260787bc1d6c8cc29bf75f534ddd42401a035a60b6b2979194cb70af5f517938b4e83066fd942e8c3cc212e0c45b989b7d4c00a0f7ad
7
- data.tar.gz: d041b0693d85f550207a0ee1a9ffc48e7ce7e5206a5471831477c1ed25b4e572a9bfb45b34f5fbb914497162d6239d817ec8abe6fff0265bf2a86e5463f54a92
6
+ metadata.gz: 03278adaba130fbe47971380cfb6c34c3835fd19d1aa8fd2df0a7f018f658ecfac25a976e642761ec4bd0c3a9b0cfd8cb06e09f5f2bb01bd080a7138514d915d
7
+ data.tar.gz: c368cf1dcb3f33d4fb6c258d810d6a197403da440f411cff62aafe43b46d02edca3424db4383ee965c5c707ee0a29e8d934653045c64573970b9bb2fd4a31883
@@ -15,7 +15,7 @@ document.AssocWhisperer = (function () {
15
15
 
16
16
  // This is where Whisperer Object is created - with all hidden inner methods.
17
17
  function createWhisperer(dom_node) {
18
- var el, w, _nodes, _timer;
18
+ var el, w, _nodes, opts, params, _timer;
19
19
 
20
20
  el = $(dom_node);
21
21
  _nodes = {
@@ -24,16 +24,23 @@ document.AssocWhisperer = (function () {
24
24
  value_field: el.children('.value_field'),
25
25
  list: null
26
26
  };
27
+ opts = el.data('opts');
28
+ if (!opts['action']) throw 'Missing action in '+JSON.stringify(opts);
29
+ if (!opts['url']) throw 'Missing url in '+JSON.stringify(opts);
30
+ if (typeof opts['params']!=='object') opts['params'] = {};
27
31
 
28
32
  w = Object.create(proto, {
29
33
  nodes: {value: _nodes},
30
- action: {value: el.attr('data-action')},
31
- url: {value: el.attr('data-url')},
34
+ action: {value: opts['action']},
35
+ url: {value: opts['url']},
36
+ params: {value: opts['params']},
32
37
  focused: {value: false, writable: true},
33
38
  filled: {value: _nodes['value_field'].val()!=='', writable: true},
34
- client_side: {value: el.attr('data-client-side')==='true'},
39
+ client_side: {value: opts['cs']===true},
40
+ preload: {value: opts['pre']===true},
35
41
  full_data: {value: null, writable: true}
36
42
  });
43
+ w.params['data_action'] = w.action;
37
44
 
38
45
  _nodes.text_field.on('keyup', function(e){
39
46
  var input_text;
@@ -101,6 +108,7 @@ document.AssocWhisperer = (function () {
101
108
  });
102
109
  }
103
110
  });
111
+ el.children('.dropdown_button').removeClass('querying'); // css class is set, hence gif gets pre-loaded
104
112
 
105
113
  // For local full_data finds matching rows and shows them.
106
114
  function digest(input_text) {
@@ -179,10 +187,11 @@ document.AssocWhisperer = (function () {
179
187
  klass.querying = true;
180
188
  btn = w.nodes['base'].find('.dropdown_button');
181
189
  btn.addClass('querying');
190
+ w.params['input'] = input || '';
182
191
  $.ajax(w.url, {
183
192
  type: 'GET',
184
193
  dataType: 'html',
185
- data: {data_action: w.action, input: (input||'')},
194
+ data: w.params,
186
195
  error: function () { w.removeList(); },
187
196
  success: on_success,
188
197
  complete: function () {
@@ -32,28 +32,18 @@ module ActionView
32
32
  # And the 'value_field' would be filled by +manager_id+ of +worker+ associated to +task+, 'text_field' by his +full_name+
33
33
  #
34
34
  def assoc_whisperer(object_name, method, data_action, options={})
35
+ raise "Helper '#assoc_whisperer' is for Rails >= 4.x. Use '#assoc_whisperer_tag' instead." if Rails::VERSION::STRING.to_i < 4
35
36
  wrapper_assoc_whisperer_assets
36
37
 
37
- options[:url] = AssocWhisperer.def_url unless options.has_key? :url
38
38
  Tags::AssocWhispererField.new(object_name, method, self, data_action, options).render
39
39
  end
40
40
 
41
41
  def assoc_whisperer_tag(name, data_action, options = {})
42
42
  wrapper_assoc_whisperer_assets
43
43
 
44
- sanitized_id = name.to_s.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_")
45
- text_tag_name = name.to_s.dup
46
- text_tag_name.insert (text_tag_name[-1]==']' ? -2 : -1), '_txt'
47
-
48
- content = %Q(<input class="value_field" id="#{sanitized_id}" name="#{name}" type="hidden")
49
- content << %Q( value="#{options[:value]}">)
50
- content << %Q(<input autocomplete="off" class="text_field#{' unfilled' if options[:value].blank?}")
51
- content << %Q( id="#{sanitized_id}_txt" name="#{text_tag_name}" size="#{options[:size]||12}")
52
- content << %Q( type="text" value="#{options[:text]}">)
53
- content << %Q(<span class="dropdown_button">\u25BE</span>)
54
- content_tag :span, content.html_safe, 'data-url' => (options[:url]||AssocWhisperer.def_url),
55
- 'data-action' => data_action, 'data-client-side' => (options[:client_side] && 'true'),
56
- 'class' => 'assoc_whisperer'
44
+ template = AssocWhisperer::Template.new data_action, options
45
+ content_tag :span, template.simple_tag_contents(name).html_safe, 'class' => 'assoc_whisperer',
46
+ 'data-opts' => template.whisperer_options.to_json
57
47
  end
58
48
 
59
49
  private
@@ -4,21 +4,19 @@ module ActionView
4
4
  class AssocWhispererField < Base # :nodoc:
5
5
 
6
6
  def initialize(object_name, method_name, template_object, action, options={})
7
- @action = action
8
- options[:value_method] = :id unless options.has_key? :value_method
9
- options[:text_method] = :to_s unless options.has_key? :text_method
7
+ options[:value_method] = AssocWhisperer.def_value_method unless options.has_key? :value_method
8
+ options[:text_method] = AssocWhisperer.def_text_method unless options.has_key? :text_method
10
9
  super object_name, method_name, template_object, options
10
+ @template = AssocWhisperer::Template.new action, @options
11
11
  end
12
12
 
13
13
  def render
14
- content = %Q(<input class="value_field" id="#{value_tag_id}" name="#{value_tag_name}" type="hidden")
15
- content << %Q( value="#{value_tag_value}">)
16
- content << %Q(<input autocomplete="off" class="text_field#{' unfilled' unless whispered_object}")
17
- content << %Q( id="#{text_tag_id}" name="#{text_tag_name}" size="#{@options[:size]||12}")
18
- content << %Q( type="text" value="#{text_tag_value}">)
19
- content << %Q(<span class="dropdown_button">\u25BE</span>)
20
- content_tag :span, content.html_safe, 'data-url' => @options[:url], 'data-action' => @action,
21
- 'data-client-side' => (@options[:client_side] && 'true'), 'class' => 'assoc_whisperer'
14
+ contents = @template.value_field_tag value_tag_id, value_tag_name, value_tag_value
15
+ contents += @template.text_field_tag text_tag_id, text_tag_name, text_tag_value, !whispered_object
16
+ contents += @template.dropdown_button_tag
17
+
18
+ content_tag :span, contents.html_safe, 'class' => 'assoc_whisperer',
19
+ 'data-opts' => @template.whisperer_options.to_json
22
20
  end
23
21
 
24
22
  private
@@ -41,7 +39,7 @@ module ActionView
41
39
  end
42
40
 
43
41
  def text_tag_value
44
- text = @options[:text_method]
42
+ text = @template.opts[:text_method]
45
43
  text = if text.respond_to? :call
46
44
  text.call whispered_object
47
45
  else
@@ -51,13 +49,7 @@ module ActionView
51
49
  end
52
50
 
53
51
  def text_sanitized_method_name
54
- return @text_sanitized_method_name if defined? @text_sanitized_method_name
55
- wsmn = if @options[:name]
56
- "#{@options[:name]}_txt"
57
- else
58
- "#{@method_name}_txt"
59
- end
60
- @text_sanitized_method_name = wsmn.sub(/\?$/,"")
52
+ @text_sanitized_method_name ||= "#{@template.opts[:name] || @method_name}_txt".sub(/\?$/,"")
61
53
  end
62
54
 
63
55
  def value_tag_name
@@ -69,15 +61,15 @@ module ActionView
69
61
  end
70
62
 
71
63
  def value_tag_value
72
- whispered_object_attribute @options[:value_method]
64
+ whispered_object_attribute @template.opts[:value_method]
73
65
  end
74
66
 
75
67
  def value_sanitized_method_name
76
68
  return @value_sanitized_method_name if defined? @value_sanitized_method_name
77
- wsmn = if @options[:name]
78
- @options[:name].to_s
69
+ wsmn = if @template.opts[:name]
70
+ @template.opts[:name].to_s
79
71
  else
80
- "#{@method_name}_#{@options[:value_method]}"
72
+ "#{@method_name}_#{@template.opts[:value_method]}"
81
73
  end
82
74
  @value_sanitized_method_name = wsmn.sub(/\?$/,"")
83
75
  end
@@ -1,10 +1,11 @@
1
1
  # encoding: utf-8
2
- require "assoc_whisperer/version"
2
+ require 'assoc_whisperer/version'
3
+ require 'assoc_whisperer/template'
3
4
 
4
5
  module AssocWhisperer
5
6
 
6
7
  class << self
7
- attr_accessor :def_url, :attach_assets_method
8
+ attr_accessor :def_url, :attach_assets_method, :def_value_method, :def_text_method
8
9
  end
9
10
 
10
11
  module Rails
@@ -12,7 +13,23 @@ module AssocWhisperer
12
13
  end
13
14
  end
14
15
 
16
+ def self.value_field_html(id, name, value)
17
+ %(<input type="hidden" id="#{id}" name="#{name}" value="#{value}" class="value_field">)
18
+ end
19
+
20
+ def self.text_field_html(id, name, value, unfilled=true, opts={})
21
+ opts = opts.keys.inject([]){|arr, k| arr << %(#{k}="#{opts[k]}"); arr }*' '
22
+ %(<input type="text" id="#{id}" name="#{name}" value="#{value} class="text_field#{' unfilled' if unfilled}"#{opts}>)
23
+ end
24
+
25
+ def self.dropdown_button_html
26
+ @dropdown_button_html ||= "<span class=\"dropdown_button\">\u25BE</span>"
27
+ end
28
+
15
29
  end
16
30
 
17
31
  AssocWhisperer.def_url = '/whisp'
18
32
  AssocWhisperer.attach_assets_method = :attach_assoc_whisperer_assets
33
+
34
+ AssocWhisperer.def_value_method = :id
35
+ AssocWhisperer.def_text_method = :to_s
@@ -0,0 +1,53 @@
1
+ # encoding: utf-8
2
+
3
+ module AssocWhisperer
4
+ class Template
5
+ attr_reader :action, :opts
6
+
7
+ def initialize(action, opts={})
8
+ @action = action
9
+ @opts = opts
10
+ end
11
+
12
+ def simple_tag_contents(input_name)
13
+ input_name = input_name.to_s
14
+ sanitized_id = input_name.dup.delete(']').gsub(/[^-a-zA-Z0-9:.]/, "_")
15
+ text_tag_name = input_name.dup
16
+ text_tag_name.insert (text_tag_name[-1]==']' ? -2 : -1), '_txt'
17
+
18
+ contents = value_field_tag sanitized_id, input_name, @opts[:value]
19
+ contents += text_field_tag "#{sanitized_id}_txt", text_tag_name, @opts[:text], @opts[:value].blank?
20
+ contents + dropdown_button_tag
21
+ end
22
+
23
+ def whisperer_options
24
+ h = {url: (@opts[:url]||AssocWhisperer.def_url), action: @action}
25
+ h.merge cs: true, pre: !!@opts[:preload] if @opts[:client_side]
26
+ h[:params] = @opts[:params] unless @opts[:params].blank?
27
+ h
28
+ end
29
+
30
+ def value_field_tag(id, name, value)
31
+ %(<input type="hidden" id="#{id}" name="#{name}" value="#{value}" class="value_field">)
32
+ end
33
+
34
+ def text_field_tag(id, name, value, unfilled=true)
35
+ opts = @opts[:text_field]
36
+ if opts
37
+ opts[:size] ||= 12
38
+ keys_whitelist = (opts.keys & [:size, :placeholder, :maxlength, :title])
39
+ opts = keys_whitelist.inject [] do |arr, k|
40
+ arr << %(#{k}="#{opts[k]}")
41
+ arr
42
+ end
43
+ opts = opts * ' '
44
+ end
45
+ %(<input type="text" autocomplete="off" id="#{id}" name="#{name}" value="#{value}" class="text_field#{' unfilled' if unfilled}"#{opts}>)
46
+ end
47
+
48
+ def dropdown_button_tag
49
+ "<span class=\"dropdown_button querying\">\u25BE</span>"
50
+ end
51
+
52
+ end
53
+ end
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
  module AssocWhisperer
3
- VERSION = "1.1.1"
3
+ VERSION = "1.2.1"
4
4
 
5
5
  end
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: 1.1.1
4
+ version: 1.2.1
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: 2014-10-02 00:00:00.000000000 Z
11
+ date: 2014-10-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,6 +60,7 @@ files:
60
60
  - app/views/assoc_whisperer/list_array.html.erb
61
61
  - assoc_whisperer.gemspec
62
62
  - lib/assoc_whisperer.rb
63
+ - lib/assoc_whisperer/template.rb
63
64
  - lib/assoc_whisperer/version.rb
64
65
  homepage: ''
65
66
  licenses: