clot_engine 1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/autotest/discover.rb +5 -0
- data/lib/clot/active_record/droppable.rb +22 -0
- data/lib/clot/base_drop.rb +116 -0
- data/lib/clot/date_tags.rb +342 -0
- data/lib/clot/deprecated.rb +11 -0
- data/lib/clot/form_filters.rb +130 -0
- data/lib/clot/form_for.rb +234 -0
- data/lib/clot/form_tag.rb +24 -0
- data/lib/clot/if_content_for.rb +32 -0
- data/lib/clot/link_filters.rb +101 -0
- data/lib/clot/model_date_tags.rb +82 -0
- data/lib/clot/model_form_tags.rb +205 -0
- data/lib/clot/mongo_mapper/droppable.rb +29 -0
- data/lib/clot/nav_bar.rb +154 -0
- data/lib/clot/no_model_form_tags.rb +233 -0
- data/lib/clot/protected.rb +25 -0
- data/lib/clot/tag_helper.rb +22 -0
- data/lib/clot/url_filters.rb +48 -0
- data/lib/clot/yield.rb +59 -0
- data/lib/clot_engine.rb +78 -0
- metadata +81 -0
@@ -0,0 +1,233 @@
|
|
1
|
+
require 'clot/tag_helper'
|
2
|
+
|
3
|
+
module Clot
|
4
|
+
module AttributeSetter
|
5
|
+
def set_primary_attributes(context)
|
6
|
+
@id_string = @name_string = resolve_value(@params.shift,context)
|
7
|
+
if @params[0] && ! @params[0].match(/:/)
|
8
|
+
@value_string = resolve_value(@params.shift,context)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def personal_attributes(name,value)
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def set_attributes(context)
|
18
|
+
set_primary_attributes(context)
|
19
|
+
|
20
|
+
@params.each do |pair|
|
21
|
+
pair.match /([^:]*):(.*)/
|
22
|
+
pair = [$1, $2]
|
23
|
+
value = resolve_value(pair[1],context)
|
24
|
+
if personal_attributes(pair[0], value)
|
25
|
+
next
|
26
|
+
end
|
27
|
+
|
28
|
+
case pair[0]
|
29
|
+
when "value" then
|
30
|
+
@value_string = value
|
31
|
+
when "type" then
|
32
|
+
@type = value
|
33
|
+
when "accept" then
|
34
|
+
@accept_string = %{accept="#{CGI::unescape value}" }
|
35
|
+
when "class" then
|
36
|
+
@class_string = %{class="#{value}" }
|
37
|
+
when "onchange" then
|
38
|
+
@onchange_string = %{onchange="#{value}" }
|
39
|
+
when "maxlength" then
|
40
|
+
@max_length_string = %{maxlength="#{value}" }
|
41
|
+
when "disabled" then
|
42
|
+
@disabled_string = %{disabled="#{if (value == true || value == "disabled") then 'disabled' end}" }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
class ClotTag < Liquid::Tag
|
49
|
+
include AttributeSetter
|
50
|
+
include TagHelper
|
51
|
+
def initialize(name, params, tokens)
|
52
|
+
@_params = split_params(params)
|
53
|
+
super
|
54
|
+
end
|
55
|
+
|
56
|
+
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
|
63
|
+
end
|
64
|
+
@params = @_params.clone
|
65
|
+
set_attributes(context)
|
66
|
+
render_string
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
class InputTag < ClotTag
|
73
|
+
|
74
|
+
def personal_attributes(name,value)
|
75
|
+
case name
|
76
|
+
when "size" then
|
77
|
+
@size_string = %{size="#{value}" }
|
78
|
+
when "width" then
|
79
|
+
@size_string = %{width="#{value}" }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def render_string
|
84
|
+
unless @value_string.nil?
|
85
|
+
@value_string = CGI::escapeHTML(@value_string.to_s)
|
86
|
+
@value_string = %{value="#{@value_string}" }
|
87
|
+
end
|
88
|
+
%{<input #{@accept_string}#{@disabled_string}#{@class_string}id="#{@id_string}" #{@max_length_string}name="#{@name_string}" #{@size_string}#{@onchange_string}type="#{@type}" #{@value_string}/>}
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
class HiddenFieldTag < InputTag
|
93
|
+
|
94
|
+
def render_string
|
95
|
+
@type = "hidden"
|
96
|
+
super
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class PasswordFieldTag < InputTag
|
101
|
+
|
102
|
+
def render_string
|
103
|
+
@type = "password"
|
104
|
+
super
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
class TextFieldTag < InputTag
|
109
|
+
|
110
|
+
def render_string
|
111
|
+
@type ||= "text"
|
112
|
+
super
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
class FileFieldTag < InputTag
|
117
|
+
|
118
|
+
def render_string
|
119
|
+
@type = "file"
|
120
|
+
super
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
class TextAreaTag < ClotTag
|
125
|
+
def personal_attributes(name,value)
|
126
|
+
|
127
|
+
case name
|
128
|
+
when "cols" then
|
129
|
+
@col_string = %{cols="#{value}" }
|
130
|
+
when "rows" then
|
131
|
+
@row_string = %{ rows="#{value}"}
|
132
|
+
when "size" then
|
133
|
+
size_array = value.split /x/
|
134
|
+
@col_string = %{cols="#{size_array[0]}" }
|
135
|
+
@row_string = %{ rows="#{size_array[1]}"}
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
def render_string
|
140
|
+
%{<textarea #{@disabled_string}#{@class_string}#{@col_string}id="#{@id_string}" name="#{@name_string}"#{@row_string}>#{@value_string}</textarea>}
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class SubmitTag < ClotTag
|
145
|
+
|
146
|
+
def personal_attributes(name,value)
|
147
|
+
case name
|
148
|
+
when "name" then
|
149
|
+
if value.nil? then @commit_name_string = '' end
|
150
|
+
when "disable_with" then
|
151
|
+
@onclick_string = %{onclick="this.disabled=true;this.value='#{value}';this.form.submit();" }
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
def set_primary_attributes(context)
|
156
|
+
@value_string = "Save changes"
|
157
|
+
@commit_name_string = 'name="commit" '
|
158
|
+
if @params[0] && ! @params[0].match(/:/)
|
159
|
+
@value_string = resolve_value @params.shift, context
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def render_string
|
164
|
+
%{<input #{@class_string}#{@onclick_string}#{@disabled_string}type="submit" #{@commit_name_string}value="#{@value_string}" />}
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
class SelectTag < ClotTag
|
170
|
+
|
171
|
+
def personal_attributes(name,value)
|
172
|
+
case name
|
173
|
+
when 'multiple' then
|
174
|
+
@multiple_string = %{multiple="#{value == "true" ? "multiple" : ""}" }
|
175
|
+
when 'prompt' then
|
176
|
+
@prompt_option = %{<option value="">#{value}</option>}
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def render_string
|
181
|
+
%{<select #{@disabled_string}#{@class_string}id="#{@id_string}" #{@multiple_string}name="#{@name_string}#{unless @multiple_string.nil? then '[]' end}">#{@prompt_option}#{@value_string}</select>}
|
182
|
+
end
|
183
|
+
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
class LabelTag < ClotTag
|
188
|
+
def render_string
|
189
|
+
@value_string ||= @name_string.humanize
|
190
|
+
%{<label #{@class_string}for="#{@id_string}">#{@value_string}</label>}
|
191
|
+
end
|
192
|
+
|
193
|
+
def personal_attributes(name,value)
|
194
|
+
case name
|
195
|
+
when 'value' then
|
196
|
+
@id_string << "_#{value}"
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
class CheckBoxTag < ClotTag
|
203
|
+
def personal_attributes(name,value)
|
204
|
+
case name
|
205
|
+
when 'collection' then
|
206
|
+
@checkbox_collection = value
|
207
|
+
when 'member' then
|
208
|
+
@checkbox_member = value
|
209
|
+
if (! @checkbox_collection.nil?) && @checkbox_collection.include?(@checkbox_member)
|
210
|
+
@checked_value = %{checked="checked" }
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
|
216
|
+
def set_primary_attributes(context)
|
217
|
+
super context
|
218
|
+
if @params[0] && ! @params[0].match(/:/)
|
219
|
+
checked = resolve_value @params.shift, context
|
220
|
+
if checked
|
221
|
+
@checked_value = %{checked="checked" }
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
def render_string
|
227
|
+
@value_string ||= 1
|
228
|
+
%{<input #{@disabled_string}#{@class_string}#{@checked_value}id="#{@id_string}" name="#{@name_string}" type="checkbox" value="#{@value_string}" />}
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
|
233
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Protected
|
2
|
+
extend ERB::Util
|
3
|
+
extend ActionView::Helpers
|
4
|
+
extend ActionView::Context
|
5
|
+
|
6
|
+
|
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
|
11
|
+
|
12
|
+
def config=(controller)
|
13
|
+
@controller = controller
|
14
|
+
end
|
15
|
+
|
16
|
+
def config
|
17
|
+
@controller
|
18
|
+
end
|
19
|
+
|
20
|
+
def controller
|
21
|
+
@controller
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Clot
|
2
|
+
module TagHelper
|
3
|
+
def split_params(params)
|
4
|
+
params.split(",").map(&:strip)
|
5
|
+
end
|
6
|
+
|
7
|
+
def resolve_value(value,context)
|
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]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module Clot
|
2
|
+
module UrlFilters
|
3
|
+
|
4
|
+
#get url from object
|
5
|
+
def object_url(target, class_name = "")
|
6
|
+
if (class_name.blank?)
|
7
|
+
class_name = target.dropped_class.to_s.tableize
|
8
|
+
end
|
9
|
+
class_name + "/" + target.id.to_s
|
10
|
+
end
|
11
|
+
|
12
|
+
#get url from object and nested object
|
13
|
+
def get_nested_url(target, nested_target, class_name = "", nested_class_name = "")
|
14
|
+
child_url = (nested_target.kind_of? String) ? nested_target : object_url(nested_target, nested_class_name)
|
15
|
+
object_url(target, class_name) + child_url
|
16
|
+
end
|
17
|
+
|
18
|
+
#get url from object and nested object
|
19
|
+
def get_nested_edit_url(target, nested_target, class_name = "", nested_class_name = "")
|
20
|
+
object_url(target, class_name) + object_url(nested_target, nested_class_name) + "/edit"
|
21
|
+
end
|
22
|
+
|
23
|
+
def stylesheet_url(sheetname)
|
24
|
+
url = "/stylesheets/" + sheetname + ".css"
|
25
|
+
url
|
26
|
+
end
|
27
|
+
|
28
|
+
def edit_link(obj, text="Edit")
|
29
|
+
url = ( obj.kind_of?( Liquid::Drop ) ? object_url( obj ) : obj )
|
30
|
+
"<a href=\"#{url}/edit\">#{text}</a>"
|
31
|
+
end
|
32
|
+
|
33
|
+
def delete_link(obj, text="Delete")
|
34
|
+
url = ( obj.kind_of?( Liquid::Drop ) ? object_url( obj ) : obj )
|
35
|
+
"<a href=\"#{url}/delete\">#{text}</a>"
|
36
|
+
end
|
37
|
+
|
38
|
+
def view_link(obj, text=nil)
|
39
|
+
url = ( obj.kind_of?( Liquid::Drop ) ? object_url( obj ) : obj )
|
40
|
+
"<a href=\"#{url}\">#{text ? text : url.capitalize}</a>"
|
41
|
+
end
|
42
|
+
|
43
|
+
def index_link(obj, text=nil)
|
44
|
+
url = ( obj.kind_of?( Liquid::Drop ) ? object_url( obj ) : obj )
|
45
|
+
"<a href=\"/#{url}\">#{text ? text : url.capitalize+' Index'}</a>"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/clot/yield.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
module Clot
|
2
|
+
class Yield < Liquid::Tag
|
3
|
+
include Liquid
|
4
|
+
|
5
|
+
Syntax = /(#{QuotedFragment}+)(\s+(?:with|for)\s+(#{QuotedFragment}+))?/
|
6
|
+
|
7
|
+
def initialize(tag_name, markup, tokens)
|
8
|
+
@blocks = []
|
9
|
+
@naked_yield = false
|
10
|
+
|
11
|
+
if markup =~ Syntax
|
12
|
+
|
13
|
+
@template_name = $1
|
14
|
+
@variable_name = $3
|
15
|
+
@attributes = {}
|
16
|
+
|
17
|
+
markup.scan(TagAttributes) do |key, value|
|
18
|
+
@attributes[key] = value
|
19
|
+
end
|
20
|
+
elsif !( markup =~ /\w/ )
|
21
|
+
@naked_yield = true
|
22
|
+
else
|
23
|
+
raise SyntaxError.new("Syntax error in tag 'yield' - Valid syntax: yield '[template]' (with|for) [object|collection]")
|
24
|
+
end
|
25
|
+
|
26
|
+
super
|
27
|
+
end
|
28
|
+
|
29
|
+
def render(context)
|
30
|
+
if @naked_yield
|
31
|
+
return context['content_for_layout']
|
32
|
+
else
|
33
|
+
partial = Liquid::Template.load_theme(context, @template_name)
|
34
|
+
#source = Liquid::Template.file_system.read_template_file( "#{context['controller_name']}/#{context['action_name']}/#{@template_name}")
|
35
|
+
#partial = Liquid::Template.parse(source)
|
36
|
+
|
37
|
+
variable = context[@variable_name || @template_name[1..-2]]
|
38
|
+
|
39
|
+
context.stack do
|
40
|
+
@attributes.each do |key, value|
|
41
|
+
context[key] = context[value]
|
42
|
+
end
|
43
|
+
|
44
|
+
if variable.is_a?(Array)
|
45
|
+
|
46
|
+
variable.collect do |variable|
|
47
|
+
context[@template_name[1..-2]] = variable
|
48
|
+
partial.render(context)
|
49
|
+
end
|
50
|
+
|
51
|
+
else
|
52
|
+
context[@template_name[1..-2]] = variable
|
53
|
+
partial.render(context)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
data/lib/clot_engine.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
module Clot
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
isolate_namespace Clot::Engine
|
4
|
+
|
5
|
+
engine_name :clot
|
6
|
+
|
7
|
+
def self.activate
|
8
|
+
%w(
|
9
|
+
clot/active_record/droppable.rb
|
10
|
+
clot/base_drop.rb
|
11
|
+
clot/if_content_for.rb
|
12
|
+
clot/date_tags.rb
|
13
|
+
clot/deprecated.rb
|
14
|
+
clot/form_for.rb
|
15
|
+
clot/form_tag.rb
|
16
|
+
clot/model_date_tags.rb
|
17
|
+
clot/model_form_tags.rb
|
18
|
+
clot/mongo_mapper/droppable.rb
|
19
|
+
clot/no_model_form_tags.rb
|
20
|
+
clot/url_filters.rb
|
21
|
+
clot/yield.rb
|
22
|
+
).each do |c|
|
23
|
+
Rails.configuration.cache_classes ? require(File.join(Clot::Engine.root, 'lib', c)) : load(File.join(Clot::Engine.root, 'lib', c))
|
24
|
+
end
|
25
|
+
|
26
|
+
Liquid::Template.register_filter Clot::UrlFilters
|
27
|
+
Liquid::Template.register_filter Clot::LinkFilters
|
28
|
+
Liquid::Template.register_filter Clot::FormFilters
|
29
|
+
|
30
|
+
Liquid::Template.register_tag('error_messages_for', Clot::ErrorMessagesFor)
|
31
|
+
Liquid::Template.register_tag('formfor', Clot::LiquidFormFor)
|
32
|
+
Liquid::Template.register_tag('form_for', Clot::LiquidFormFor)
|
33
|
+
Liquid::Template.register_tag('yield', Clot::Yield)
|
34
|
+
Liquid::Template.register_tag('if_content_for', Clot::IfContentFor)
|
35
|
+
Liquid::Template.register_tag('form_tag', Clot::FormTag)
|
36
|
+
|
37
|
+
Liquid::Template.register_tag('select_tag', Clot::SelectTag)
|
38
|
+
Liquid::Template.register_tag('text_field_tag', Clot::TextFieldTag)
|
39
|
+
Liquid::Template.register_tag('hidden_field_tag', Clot::HiddenFieldTag)
|
40
|
+
Liquid::Template.register_tag('file_field_tag', Clot::FileFieldTag)
|
41
|
+
Liquid::Template.register_tag('text_area_tag', Clot::TextAreaTag)
|
42
|
+
Liquid::Template.register_tag('submit_tag', Clot::SubmitTag)
|
43
|
+
Liquid::Template.register_tag('label_tag', Clot::LabelTag)
|
44
|
+
Liquid::Template.register_tag('check_box_tag', Clot::CheckBoxTag)
|
45
|
+
Liquid::Template.register_tag('password_field_tag', Clot::PasswordFieldTag)
|
46
|
+
|
47
|
+
Liquid::Template.register_tag('text_field', Clot::TextField)
|
48
|
+
Liquid::Template.register_tag('text_area', Clot::TextArea)
|
49
|
+
Liquid::Template.register_tag('label', Clot::Label)
|
50
|
+
Liquid::Template.register_tag('check_box', Clot::CheckBox)
|
51
|
+
Liquid::Template.register_tag('collection_select', Clot::CollectionSelect)
|
52
|
+
Liquid::Template.register_tag('file_field', Clot::FileField)
|
53
|
+
Liquid::Template.register_tag('password_field', Clot::PasswordField)
|
54
|
+
|
55
|
+
Liquid::Template.register_tag('select_second', Clot::SelectSecond)
|
56
|
+
Liquid::Template.register_tag('select_minute', Clot::SelectMinute)
|
57
|
+
Liquid::Template.register_tag('select_hour', Clot::SelectHour)
|
58
|
+
|
59
|
+
Liquid::Template.register_tag('select_day', Clot::SelectDay)
|
60
|
+
Liquid::Template.register_tag('select_month', Clot::SelectMonth)
|
61
|
+
Liquid::Template.register_tag('select_year', Clot::SelectYear)
|
62
|
+
|
63
|
+
Liquid::Template.register_tag('select_date', Clot::SelectDate)
|
64
|
+
Liquid::Template.register_tag('select_time', Clot::SelectTime)
|
65
|
+
Liquid::Template.register_tag('select_datetime', Clot::SelectDatetime)
|
66
|
+
|
67
|
+
Liquid::Template.register_tag('date_select', Clot::DateSelect)
|
68
|
+
Liquid::Template.register_tag('time_select', Clot::TimeSelect)
|
69
|
+
Liquid::Template.register_tag('datetime_select', Clot::DatetimeSelect)
|
70
|
+
|
71
|
+
::ActiveRecord::Base.send(:include, Clot::ActiveRecord::Droppable)
|
72
|
+
#::MongoMapper::Document.send(:include, Clot::MongoMapper::Droppable)
|
73
|
+
end
|
74
|
+
|
75
|
+
config.to_prepare &method(:activate).to_proc
|
76
|
+
|
77
|
+
end
|
78
|
+
end
|