combo_box 0.0.8 → 0.0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/VERSION +1 -1
  2. data/lib/combo_box/generator.rb +17 -18
  3. metadata +10 -10
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.8
1
+ 0.0.9
@@ -98,11 +98,13 @@ module ComboBox
98
98
 
99
99
  query = []
100
100
  parameters = ''
101
+ initial_conditions = ""
101
102
  if @options[:conditions].is_a? Hash
102
103
  @options[:conditions].each do |key, value|
103
104
  query << (key.is_a?(Symbol) ? @model.table_name+"."+key.to_s : key.to_s)+'=?'
104
105
  parameters += ', ' + sanitize_conditions(value)
105
106
  end
107
+ initial_conditions = query.join(' AND ').inspect+parameters
106
108
  elsif @options[:conditions].is_a? Array
107
109
  conditions = @options[:conditions]
108
110
  if conditions[0].is_a?(String) # SQL
@@ -111,22 +113,22 @@ module ComboBox
111
113
  else
112
114
  raise Exception.new("First element of an Array can only be String or Symbol.")
113
115
  end
116
+ initial_conditions = query.join(' AND ').inspect+parameters
117
+ elsif @options[:conditions].is_a? String
118
+ initial_conditions = "("+@options[:conditions].gsub(/\s*\n\s*/, ';')+")"
119
+ else !@options[:conditions].nil?
120
+ raise ArgumentError.new("Option :conditions can be a Hash, an Array or String")
114
121
  end
115
122
 
116
- # select = "#{@model.table_name}.id AS id"
117
- # for c in @columns
118
- # select << ", #{c.sql_name} AS #{c.short_name}"
119
- # end
120
-
121
123
  code = ""
122
- code << "search, conditions = params[:term], [#{query.join(' AND ').inspect+parameters}]\n"
124
+ code << "search, conditions = params[:term], []\n"
123
125
  code << "if params[:id].to_i > 0\n"
124
- code << " conditions[0] << '#{' AND ' if query.size>0}#{@model.table_name}.id = ?'\n"
126
+ code << " conditions[0] = '#{@model.table_name}.id = ?'\n"
125
127
  code << " conditions << params[:id].to_i\n"
126
128
  code << "else\n"
127
129
  code << " words = search.to_s.mb_chars.downcase.strip.normalize.split(/[\\s\\,]+/)\n"
128
130
  code << " if words.size > 0\n"
129
- code << " conditions[0] << '#{' AND ' if query.size>0}('\n"
131
+ code << " conditions[0] = '('\n"
130
132
  code << " words.each_index do |index|\n"
131
133
  code << " word = words[index].to_s\n"
132
134
  code << " conditions[0] << ') AND (' if index > 0\n"
@@ -141,20 +143,14 @@ module ComboBox
141
143
  code << " end\n"
142
144
  code << "end\n"
143
145
 
144
- # joins = @options[:joins] ? ", :joins=>"+@options[:joins].inspect : ""
145
- # order = ", :order=>"+@columns.collect{|column| "#{column[0]} ASC"}.join(', ').inspect
146
- # limit = ", :limit=>"+(@options[:limit]||80).to_s
147
- joins = @options[:joins] ? ".joins(#{@options[:joins].inspect}).includes(#{@options[:joins].inspect})" : ""
148
-
149
- order = @options[:order] ? ".order(#{@options[:order].inspect})" : ".order("+@columns.collect{|c| "#{c.sql_name} ASC"}.join(', ').inspect+")"
146
+ joins = (@options[:joins] ? ".joins(#{@options[:joins].inspect}).includes(#{@options[:joins].inspect})" : "")
147
+ order = (@options[:order] ? ".order(#{@options[:order].inspect})" : ".order("+@columns.collect{|c| "#{c.sql_name} ASC"}.join(', ').inspect+")")
150
148
  limit = ".limit(#{@options[:limit]||80})"
151
149
 
152
150
  partial = @options[:partial]
153
151
 
154
152
  html = "<ul><% for #{foreign_record} in #{foreign_records} -%><li id='<%=#{foreign_record}.id-%>'>"
155
153
  html << "<% content = item_label_for_#{@action_name}_in_#{@controller.controller_name}(#{foreign_record})-%>"
156
- # html << "<%content="+#{foreign_record}.#{field.item_label}+" -%>"
157
- # html << "<%content="+@columns.collect{|column| "#{foreign_record}['#{column[2]}'].to_s"}.join('+", "+')+" -%>"
158
154
  if partial
159
155
  html << "<%=render(:partial=>#{partial.inspect}, :locals =>{:#{foreign_record}=>#{foreign_record}, :content=>content, :search=>search})-%>"
160
156
  else
@@ -162,14 +158,17 @@ module ComboBox
162
158
  end
163
159
  html << '</li><%end-%></ul>'
164
160
 
165
- code << "#{foreign_records} = #{@model.name}.where(conditions)"+joins+order+limit+"\n"
161
+ code << "#{foreign_records} = #{@model.name}"
162
+ code << ".where(#{initial_conditions})" unless initial_conditions.blank?
163
+ code << ".where(conditions)"+joins+order+limit+"\n"
166
164
  # Render HTML is old Style
167
165
  code << "respond_to do |format|\n"
168
166
  code << " format.html { render :inline=>#{html.inspect}, :locals=>{:#{foreign_records}=>#{foreign_records}, :search=>search} }\n"
169
167
  code << " format.json { render :json=>#{foreign_records}.collect{|#{foreign_record}| {:label=>#{item_label(foreign_record)}, :id=>#{foreign_record}.id}}.to_json }\n"
170
168
  code << " format.yaml { render :yaml=>#{foreign_records}.collect{|#{foreign_record}| {'label'=>#{item_label(foreign_record)}, 'id'=>#{foreign_record}.id}}.to_yaml }\n"
171
- code << " format.xml { render :xml=>#{foreign_records}.collect{|#{foreign_record}| {:label=>#{item_label(foreign_record)}, :id=>#{foreign_record}.id}}.to_xml }\n"
169
+ code << " format.xml { render :xml=>#{foreign_records}.collect{|#{foreign_record}| {:label=>#{item_label(foreign_record)}, :id=>#{foreign_record}.id}}.to_xml }\n"
172
170
  code << "end\n"
171
+ puts code
173
172
  return code
174
173
  end
175
174
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: combo_box
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-28 00:00:00.000000000Z
12
+ date: 2012-03-29 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
16
- requirement: &23382560 !ruby/object:Gem::Requirement
16
+ requirement: &23634220 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *23382560
24
+ version_requirements: *23634220
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: jquery-rails
27
- requirement: &23381940 !ruby/object:Gem::Requirement
27
+ requirement: &23633580 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *23381940
35
+ version_requirements: *23633580
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: jeweler
38
- requirement: &23381300 !ruby/object:Gem::Requirement
38
+ requirement: &23620840 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 1.6.4
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *23381300
46
+ version_requirements: *23620840
47
47
  description: Adds helpers for Rails views and controller in order to manage 'dynamic
48
48
  select'. It uses jQuery UI as support for inobtrusive use in forms. It's not the
49
49
  classic Autocompleter, its use is limited to belongs_to reflections.
@@ -82,7 +82,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
82
82
  version: '0'
83
83
  segments:
84
84
  - 0
85
- hash: 2856060928740621467
85
+ hash: -3822598672514359914
86
86
  required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  none: false
88
88
  requirements:
@@ -91,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
91
  version: '0'
92
92
  requirements: []
93
93
  rubyforge_project:
94
- rubygems_version: 1.8.11
94
+ rubygems_version: 1.8.10
95
95
  signing_key:
96
96
  specification_version: 3
97
97
  summary: Adds the combo_box control in Rails 3 based on jQuery UI