clot_engine 1.2 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -88,8 +88,8 @@ module Clot
88
88
  @source.errors
89
89
  end
90
90
 
91
- def collection_label
92
- "label field"
91
+ def new_record
92
+ @source.new_record?
93
93
  end
94
94
 
95
95
  def self.method_missing(symbol, *args)
data/lib/clot/form_for.rb CHANGED
@@ -20,7 +20,7 @@ module Clot
20
20
  @attributes[key] = value
21
21
  end
22
22
  else
23
- syntax_error tag_name, markup, tokens
23
+ syntax_error# tag_name, markup, tokens
24
24
  end
25
25
  super
26
26
  end
@@ -81,7 +81,7 @@ module Clot
81
81
  @attributes[key] = value
82
82
  end
83
83
  else
84
- syntax_error tag_name, markup, tokens
84
+ syntax_error# tag_name, markup, tokens
85
85
  end
86
86
  super
87
87
  end
@@ -91,7 +91,11 @@ module Clot
91
91
  if @attributes.has_key?('url_helper')
92
92
  Protected.config = context.registers[:controller]
93
93
  @attributes['url'] = Protected.send(@attributes['url_helper'].to_sym) unless @attributes.has_key?('url_helper_params')
94
- @attributes['url'] = Protected.send(@attributes['url_helper'].to_sym, context[@attributes['url_helper_params']].source) if @attributes.has_key?('url_helper_params')
94
+ if params = context[@attributes['url_helper_params']]
95
+ arg = params.is_a?(String) ? params : params.source
96
+ @attributes['url'] = Protected.send(@attributes['url_helper'].to_sym, arg)
97
+ end
98
+
95
99
  end
96
100
 
97
101
  if @attributes.has_key?('html')
@@ -195,7 +199,6 @@ module Clot
195
199
  @params = @_params.clone
196
200
  @model = context[@params.shift]
197
201
 
198
- result = ""
199
202
  if @model and @model.respond_to?(:errors) and @model.errors.count > 0
200
203
  @suffix = @model.errors.count > 1 ? "s" : ""
201
204
  @default_message = @model.errors.count.to_s + " error#{@suffix} occurred while processing information"
@@ -210,25 +213,11 @@ module Clot
210
213
  end
211
214
  end
212
215
 
213
- result += '<div class="errorExplanation" id="errorExplanation"><h2>' + @default_message + '</h2><ul>'
214
-
215
- @model.errors.each do |attr, msg|
216
- result += "<li>#{error_message(attr, msg)}</li>"
217
- end if @model.respond_to? :errors
216
+ readable_errors = @model.errors.map{ |target, msg| "#{target}: #{msg}" }
218
217
 
219
- result += "</ul></div>"
218
+ { :message => @default_message, :errors => readable_errors }.to_json
220
219
  end
221
- result
222
220
  end
223
-
224
- def error_message(attr, msg)
225
- unless attr == :base
226
- "#{attr} - #{msg}"
227
- else
228
- msg
229
- end
230
- end
231
-
232
221
  end
233
222
 
234
223
  end
@@ -3,7 +3,6 @@ module Clot
3
3
  def set_primary_attributes(context)
4
4
  @item = context['form_model']
5
5
  if @item
6
-
7
6
  @attribute_name = resolve_value(@params.shift,context)
8
7
  @first_attr = context['form_class_name']
9
8
  else
@@ -50,15 +49,9 @@ module Clot
50
49
  end
51
50
 
52
51
  def render(context)
53
- result = super(context)
54
- #if @errors.include? @attribute_name
55
- # result = "<div class=\"fieldWithErrors\">#{result}</div>"
56
- #end
57
- result
52
+ super(context)
58
53
  end
59
54
 
60
-
61
-
62
55
  end
63
56
 
64
57
  class FileField < FileFieldTag
@@ -99,84 +92,22 @@ module Clot
99
92
  end
100
93
 
101
94
  class CollectionSelect < ClotTag
102
- include ModelTag
103
- def set_primary_attributes(context)
104
- super context
105
- if @params[0] && ! @params[0].match(/:/)
106
- @collection = resolve_value(@params.shift,context)
107
- end
108
- @default_id = 'id'
109
- @default_name = 'name'
110
- if @params[0] && ! @params[0].match(/:/)
111
- @default_id = resolve_value(@params.shift,context)
112
- end
113
- if @params[0] && ! @params[0].match(/:/)
114
- @default_name = resolve_value(@params.shift,context)
115
- end
116
- end
95
+ # usage:
96
+ # {% collection_select "order[bill_address_attributes]" method:"country_id", collection:available_countries, value_method:'id', text_method:'name', html_options:"{class:'large-field'}" %}
117
97
 
118
- def gen_option(item)
119
- selection_string = ""
120
- item_string = item
121
- value_string = ""
122
-
123
- # this line below is for BSON::ObjectId which doesn't respond to :id, but does :_id
124
- @default_id = "_id" if @default_id == 'id' and item.respond_to?(:_id)
125
-
126
- # @item = NationSignupDrop
127
- # item = NationPlan
128
-
129
- attribute_names = @attribute_name.split('.')
130
-
131
- if item.is_a?(String) || item.is_a?(Fixnum) || item.is_a?(Float)
132
- if (@item[@attribute_name.to_sym].to_s == item.to_s) || (@item.respond_to?(@attribute_name.to_sym) && @item.send(@attribute_name.to_sym).to_s == item.to_s)
133
- selection_string = ' selected="selected"'
134
- end
135
- else
136
- item_string = item[@default_name.to_sym] || (@item.respond_to?(@attribute_name.to_sym) && @item.send(@default_name.to_sym))
137
- value_string = %{ value="#{item[@default_id.to_sym]}"}
138
- if @item.class.to_s == "NationSignupDrop" and attribute_names.size == 3 and attribute_names[0] == 'payment_profile' # this is a special case just for 3dna nation signup mongo stuff
139
- if attribute_names[1] == 'billing_address'
140
- if item[@default_id.to_sym].to_s == @item.source.payment_profile.billing_address[attribute_names[2].to_sym].to_s
141
- selection_string = ' selected="selected"'
142
- end
143
- else
144
- if item[@default_id.to_sym].to_s == @item.source.payment_profile[attribute_names[1].to_sym][attribute_names[2].to_sym].to_s
145
- selection_string = ' selected="selected"'
146
- end
147
- end
148
- elsif attribute_names.size == 3
149
- if item[@default_id.to_sym].to_s == @item[attribute_names[0].to_sym][attribute_names[1].to_sym][attribute_names[2].to_sym].to_s
150
- selection_string = ' selected="selected"'
151
- end
152
- elsif attribute_names.size == 2
153
- if item[@default_id.to_sym].to_s == @item[attribute_names.first.to_sym][attribute_names.last.to_sym].to_s
154
- selection_string = ' selected="selected"'
155
- end
156
- else
157
- if item[@default_id.to_sym].to_s == @item[@attribute_name.to_sym].to_s
158
- selection_string = ' selected="selected"'
159
- end
160
- end
161
- end
162
-
163
- "<option#{value_string}#{selection_string}>#{item_string}</option>"
164
- end
98
+ def render(context)
99
+ super(context)
165
100
 
166
- def personal_attributes(name,value)
167
- case name
168
- when 'prompt' then
169
- @prompt_option = %{<option value="">#{value}</option>}
170
- end
171
- end
101
+ # collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
102
+ method = @attributes['method']
103
+ collection = @attributes['collection']
104
+ value_method = @attributes['value_method']
105
+ text_method = @attributes['text_method']
106
+ @attributes.has_key?('options') ? options = @attributes['options'] : options = {}
107
+ @attributes.has_key?('html_options') ? html_options = @attributes['html_options'] : html_options = {}
172
108
 
173
- def render_string
174
- @option_string = "#{@prompt_option}"
175
- @collection.each do |item|
176
- @option_string << gen_option(item)
177
- end
178
109
 
179
- %{<select id="#{@id_string}" name="#{@name_string}">#{@option_string}</select>}
110
+ context.registers[:action_view].collection_select(@form_object, method, collection, value_method, text_method, options, html_options)
180
111
  end
181
112
  end
182
113
 
@@ -46,24 +46,31 @@ module Clot
46
46
  end
47
47
 
48
48
  class ClotTag < Liquid::Tag
49
+ Syntax = /([^\s]+)\s+/
50
+
49
51
  include AttributeSetter
50
52
  include TagHelper
51
- def initialize(name, params, tokens)
52
- @_params = split_params(params)
53
+
54
+ def initialize(tag_name, markup, tokens)
55
+ if markup =~ Syntax
56
+ @form_object = $1
57
+ @attributes = {}
58
+ markup.scan(Liquid::TagAttributes) do |key, value|
59
+ @attributes[key] = value
60
+ end
61
+ else
62
+ syntax_error tag_name, markup, tokens
63
+ end
53
64
  super
54
65
  end
55
66
 
56
67
  def render(context)
57
-
58
-
59
- instance_variables.map(&:to_sym).each do |var|
60
- unless [:@_params, :@markup, :@tag_name].include? var
61
- instance_variable_set var, nil #this is because the same parse tag is re-rendered
62
- end
68
+ @attributes.each do |key, value|
69
+ @attributes[key] = resolve_value(value, context)
63
70
  end
64
- @params = @_params.clone
65
- set_attributes(context)
66
- render_string
71
+
72
+ @form_object = context[@form_object].to_sym if context[@form_object].is_a?(String)
73
+ @form_object = context[@form_object].source if @form_object.nil? && context[@form_object].is_a?(Liquid::Drop)
67
74
  end
68
75
 
69
76
  end
@@ -5,9 +5,10 @@ module Protected
5
5
 
6
6
 
7
7
  class << self
8
- include Rails.application.routes.url_helpers
9
- include Refinery::Core::Engine.routes.url_helpers
10
- include Spree::Core::Engine.routes.url_helpers
8
+ #include Spree::Core::Engine.routes.url_helpers
9
+ #include Refinery::Core::Engine.routes.url_helpers
10
+ #include Rails.application.routes.url_helpers
11
+ #include ActionView::Helpers::UrlHelper
11
12
 
12
13
  def config=(controller)
13
14
  @controller = controller
@@ -21,5 +22,4 @@ module Protected
21
22
  @controller
22
23
  end
23
24
  end
24
-
25
25
  end
@@ -4,17 +4,30 @@ module Clot
4
4
  params.split(",").map(&:strip)
5
5
  end
6
6
 
7
- def resolve_value(value,context)
7
+ def resolve_value(value, context)
8
8
  case value
9
- when /^([\[])(.*)([\]])$/ then array = $2.split " "; array.map { |item| resolve_value item, context }
10
- when /^(["'])(.*)\1$/ then $2
11
- when /^(\d+[\.]\d+)$/ then $1.to_f
12
- when /^(\d+)$/ then value.to_i
13
- when /^true$/ then true
14
- when /^false$/ then false
15
- when /^nil$/ then nil
16
- when /^(.+)_path$/ then "/#{$1}"
17
- else context[value]
9
+ when Liquid::Drop then
10
+ value.source
11
+ when /^([\[])(.*)([\]])$/ then
12
+ array = $2.split " "; array.map { |item| resolve_value item, context }
13
+ when /^"(\{.*\})"$/ then
14
+ eval($1) # hash from string
15
+ when /^(["'])(.*)\1$/ then
16
+ $2
17
+ when /^(\d+[\.]\d+)$/ then
18
+ $1.to_f
19
+ when /^(\d+)$/ then
20
+ value.to_i
21
+ when /^true$/ then
22
+ true
23
+ when /^false$/ then
24
+ false
25
+ when /^nil$/ then
26
+ nil
27
+ when /^(.+)_path$/ then
28
+ "/#{$1}"
29
+ else
30
+ context[value]
18
31
  end
19
32
  end
20
33
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clot_engine
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: 1.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,24 +9,8 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-03 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: mongo_mapper
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: 0.12.0
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: 0.12.0
12
+ date: 2013-03-31 00:00:00.000000000 Z
13
+ dependencies: []
30
14
  description: Extensions for solidifying Liquid Templates
31
15
  email:
32
16
  - alexander.negoda@gmail.com