right-rails 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.textile +50 -0
- data/Rakefile +23 -0
- data/generators/right_rails/right_rails_generator.rb +41 -0
- data/generators/right_rails/templates/iframed.html.erb +10 -0
- data/generators/right_scaffold/right_scaffold_generator.rb +53 -0
- data/generators/right_scaffold/templates/controller.rb +99 -0
- data/generators/right_scaffold/templates/helper.rb +2 -0
- data/generators/right_scaffold/templates/layout.html.erb +18 -0
- data/generators/right_scaffold/templates/style.css +54 -0
- data/generators/right_scaffold/templates/view__form.html.erb +16 -0
- data/generators/right_scaffold/templates/view__item.html.erb +13 -0
- data/generators/right_scaffold/templates/view_edit.html.erb +6 -0
- data/generators/right_scaffold/templates/view_index.html.erb +9 -0
- data/generators/right_scaffold/templates/view_new.html.erb +5 -0
- data/generators/right_scaffold/templates/view_show.html.erb +10 -0
- data/init.rb +12 -0
- data/javascripts/right-autocompleter-src.js +303 -0
- data/javascripts/right-autocompleter.js +9 -0
- data/javascripts/right-behavior-src.js +240 -0
- data/javascripts/right-behavior.js +8 -0
- data/javascripts/right-calendar-src.js +855 -0
- data/javascripts/right-calendar.js +9 -0
- data/javascripts/right-dnd-src.js +555 -0
- data/javascripts/right-dnd.js +9 -0
- data/javascripts/right-effects-src.js +425 -0
- data/javascripts/right-effects.js +6 -0
- data/javascripts/right-events-src.js +369 -0
- data/javascripts/right-events.js +6 -0
- data/javascripts/right-json-src.js +176 -0
- data/javascripts/right-json.js +6 -0
- data/javascripts/right-lightbox-src.js +597 -0
- data/javascripts/right-lightbox.js +9 -0
- data/javascripts/right-rails-src.js +269 -0
- data/javascripts/right-rails.js +9 -0
- data/javascripts/right-rater-src.js +248 -0
- data/javascripts/right-rater.js +9 -0
- data/javascripts/right-selectable-src.js +507 -0
- data/javascripts/right-selectable.js +7 -0
- data/javascripts/right-slider-src.js +291 -0
- data/javascripts/right-slider.js +7 -0
- data/javascripts/right-sortable-src.js +221 -0
- data/javascripts/right-sortable.js +9 -0
- data/javascripts/right-src.js +4939 -0
- data/javascripts/right-tabs-src.js +776 -0
- data/javascripts/right-tabs.js +6 -0
- data/javascripts/right-tooltips-src.js +130 -0
- data/javascripts/right-tooltips.js +9 -0
- data/javascripts/right-ui-i18n-de.js +29 -0
- data/javascripts/right-ui-i18n-en-us.js +11 -0
- data/javascripts/right-ui-i18n-es.js +29 -0
- data/javascripts/right-ui-i18n-fr.js +29 -0
- data/javascripts/right-ui-i18n-jp.js +33 -0
- data/javascripts/right-ui-i18n-ru.js +29 -0
- data/javascripts/right-ui-i18n-uk.js +29 -0
- data/javascripts/right.js +10 -0
- data/lib/right-rails.rb +11 -0
- data/lib/right_rails/controller_extensions.rb +85 -0
- data/lib/right_rails/helpers/basic.rb +111 -0
- data/lib/right_rails/helpers/forms.rb +239 -0
- data/lib/right_rails/helpers/misc.rb +164 -0
- data/lib/right_rails/helpers/rails.rb +166 -0
- data/lib/right_rails/helpers.rb +5 -0
- data/lib/right_rails/java_script_generator.rb +313 -0
- data/lib/right_rails.rb +6 -0
- data/spec/lib/right_rails/controller_extensions_spec.rb +60 -0
- data/spec/lib/right_rails/helpers/basic_spec.rb +74 -0
- data/spec/lib/right_rails/helpers/forms_spec.rb +51 -0
- data/spec/lib/right_rails/helpers/misc_spec.rb +120 -0
- data/spec/lib/right_rails/helpers/rails_spec.rb +149 -0
- data/spec/lib/right_rails/java_script_generator_spec.rb +317 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +15 -0
- metadata +128 -0
@@ -0,0 +1,313 @@
|
|
1
|
+
#
|
2
|
+
# The right-rails scripts generator
|
3
|
+
#
|
4
|
+
class RightRails::JavaScriptGenerator
|
5
|
+
|
6
|
+
def initialize(template, thread=nil)
|
7
|
+
@util = Util.new(template, thread)
|
8
|
+
end
|
9
|
+
|
10
|
+
# the top-level constants that the generator should respond to transparently
|
11
|
+
JS_CONSTANTS = [:document, :window, :top, :RR]
|
12
|
+
|
13
|
+
# method calls catchup
|
14
|
+
def method_missing(name, *args)
|
15
|
+
@util.record(if JS_CONSTANTS.include?(name)
|
16
|
+
name
|
17
|
+
elsif name.to_s[name.to_s.size-1, name.to_s.size] == '='
|
18
|
+
"#{name.to_s[0, name.to_s.size-1]}=#{@util.to_js_type(args.first)}"
|
19
|
+
else
|
20
|
+
"#{name}(#{@util.to_js_args(args)})"
|
21
|
+
end)
|
22
|
+
end
|
23
|
+
|
24
|
+
# returns the result script
|
25
|
+
def to_s
|
26
|
+
@util.build_script
|
27
|
+
end
|
28
|
+
|
29
|
+
#
|
30
|
+
# This module contains the predefined methods collection
|
31
|
+
#
|
32
|
+
module Methods
|
33
|
+
# referring an element by an id or a record
|
34
|
+
def [](record_or_id)
|
35
|
+
@util.record("$(\"#{@util.dom_id(record_or_id)}\")")
|
36
|
+
end
|
37
|
+
|
38
|
+
# just pushes a line of code into the thread
|
39
|
+
def << (code)
|
40
|
+
@util.write(code)
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
# builds a css-select block
|
45
|
+
def find(css_rule)
|
46
|
+
@util.record("$$(\"#{css_rule}\")")
|
47
|
+
end
|
48
|
+
|
49
|
+
# access to the javascript variables
|
50
|
+
def get(name)
|
51
|
+
@util.record(name)
|
52
|
+
end
|
53
|
+
|
54
|
+
# variables initializing method
|
55
|
+
def set(name, value)
|
56
|
+
@util.record("var #{name}=#{@util.to_js_type(value)}")
|
57
|
+
end
|
58
|
+
|
59
|
+
# generates the redirection script
|
60
|
+
def redirect_to(location)
|
61
|
+
self.document[:location].href = (location.is_a?(String) ? location : @util.template.url_for(location))
|
62
|
+
self
|
63
|
+
end
|
64
|
+
|
65
|
+
# generates the page reload script
|
66
|
+
def reload
|
67
|
+
self.document[:location].reload
|
68
|
+
self
|
69
|
+
end
|
70
|
+
|
71
|
+
# builds the record HTML code and then insterts it in place
|
72
|
+
def insert(record)
|
73
|
+
self.RR.insert(record.class.table_name, @util.render(record))
|
74
|
+
end
|
75
|
+
|
76
|
+
# generates a script that inserts the record partial, then updates the form and the flashes block
|
77
|
+
def insert_and_care(record)
|
78
|
+
insert(record)
|
79
|
+
@util.template.instance_variable_set("@#{record.class.table_name.singularize}", record.class.new)
|
80
|
+
replace_form_for(record.class.new)
|
81
|
+
update_flash
|
82
|
+
end
|
83
|
+
|
84
|
+
# replaces the record element on the page
|
85
|
+
def replace(record)
|
86
|
+
self.RR.replace(@util.dom_id(record), @util.render(record))
|
87
|
+
end
|
88
|
+
|
89
|
+
# replaces the record partial and updates the flash
|
90
|
+
def replace_and_care(record)
|
91
|
+
replace(record)
|
92
|
+
update_flash
|
93
|
+
end
|
94
|
+
|
95
|
+
# removes the record element from the page
|
96
|
+
def remove(record)
|
97
|
+
self.RR.remove(@util.dom_id(record))
|
98
|
+
end
|
99
|
+
|
100
|
+
# renders and shows a form for the record
|
101
|
+
def show_form_for(record)
|
102
|
+
self.RR.show_form_for(@util.dom_id(record), @util.render('form'))
|
103
|
+
end
|
104
|
+
|
105
|
+
# renders and updates a form for the record
|
106
|
+
def replace_form_for(record)
|
107
|
+
self.RR.replace_form(@util.form_id_for(record), @util.render('form'))
|
108
|
+
end
|
109
|
+
|
110
|
+
# updates the flashes block
|
111
|
+
def update_flash(content=nil)
|
112
|
+
self.RR.update_flash(content || @util.template.flashes)
|
113
|
+
@util.template.flash.clear
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
include Methods
|
118
|
+
|
119
|
+
protected
|
120
|
+
|
121
|
+
#
|
122
|
+
# Keeps the javascript method calls sequence and then represents iteslf like a string of javascript
|
123
|
+
#
|
124
|
+
class MethodCall
|
125
|
+
|
126
|
+
def initialize(this, util, parent)
|
127
|
+
@this = this
|
128
|
+
@util = util
|
129
|
+
@parent = parent
|
130
|
+
end
|
131
|
+
|
132
|
+
# catches the properties request
|
133
|
+
def [](name)
|
134
|
+
@child = @util.make_call(".#{name}", self)
|
135
|
+
end
|
136
|
+
|
137
|
+
# attribute assignment hook
|
138
|
+
def []=(name, value)
|
139
|
+
send "#{name}=", value
|
140
|
+
end
|
141
|
+
|
142
|
+
OPERATIONS = %w{+ - * / % <<}
|
143
|
+
|
144
|
+
# catches all the method calls
|
145
|
+
def method_missing(name, *args, &block)
|
146
|
+
name = name.to_s
|
147
|
+
args << block if block_given?
|
148
|
+
|
149
|
+
@child = @util.make_call((
|
150
|
+
# assignments
|
151
|
+
if name[name.size-1, name.size] == '='
|
152
|
+
".#{name[0,name.size-1]}=#{@util.to_js_type(args.first)}"
|
153
|
+
|
154
|
+
# operation calls
|
155
|
+
elsif OPERATIONS.include?(name)
|
156
|
+
name = "+=" if name == '<<'
|
157
|
+
"#{name}#{@util.to_js_type(args.first)}"
|
158
|
+
|
159
|
+
# usual method calls
|
160
|
+
else
|
161
|
+
".#{name}(#{@util.to_js_args(args)})"
|
162
|
+
end
|
163
|
+
), self)
|
164
|
+
end
|
165
|
+
|
166
|
+
# exports the whole thing into a javascript string
|
167
|
+
def to_s
|
168
|
+
nodes = []
|
169
|
+
node = self
|
170
|
+
|
171
|
+
while node
|
172
|
+
nodes << node
|
173
|
+
node = node.instance_variable_get(@parent.nil? ? "@child" : "@parent")
|
174
|
+
end
|
175
|
+
|
176
|
+
# reversing the calls list if building from the right end
|
177
|
+
nodes.reverse! unless @parent.nil?
|
178
|
+
|
179
|
+
nodes.collect{|n| n.instance_variable_get("@this").to_s }.join('')
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
#
|
184
|
+
# We use this class to cleanup the main namespace of the JavaScriptGenerator instances
|
185
|
+
# So that the mesod_missing didn't interferate with the util methods
|
186
|
+
#
|
187
|
+
class Util
|
188
|
+
attr_reader :template
|
189
|
+
|
190
|
+
def initialize(template, thread=nil)
|
191
|
+
@template = template
|
192
|
+
@thread = thread || []
|
193
|
+
end
|
194
|
+
|
195
|
+
# returns a conventional dom id for the record
|
196
|
+
def dom_id(record)
|
197
|
+
if [String, Symbol].include?(record.class)
|
198
|
+
"#{record}"
|
199
|
+
else
|
200
|
+
@template.dom_id(record)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
# generates the form-id for the given record
|
205
|
+
def form_id_for(record)
|
206
|
+
record.new_record? ? "new_#{record.class.table_name.singularize}" : "edit_#{dom_id(record)}"
|
207
|
+
end
|
208
|
+
|
209
|
+
# retnders the thing
|
210
|
+
def render(what)
|
211
|
+
@template.render(what)
|
212
|
+
end
|
213
|
+
|
214
|
+
# builds a new method call object
|
215
|
+
def make_call(string, parent=nil)
|
216
|
+
MethodCall.new(string, self, parent)
|
217
|
+
end
|
218
|
+
|
219
|
+
# Records a new call
|
220
|
+
def record(command)
|
221
|
+
@thread << (line = make_call(command))
|
222
|
+
line
|
223
|
+
end
|
224
|
+
|
225
|
+
# writes a pline script code into the thread
|
226
|
+
def write(script)
|
227
|
+
@thread << script
|
228
|
+
end
|
229
|
+
|
230
|
+
# builds the end script
|
231
|
+
def build_script
|
232
|
+
@thread.collect{|line|
|
233
|
+
line.is_a?(String) ? line : (line.to_s + ';')
|
234
|
+
}.join('')
|
235
|
+
end
|
236
|
+
|
237
|
+
# converts the list of values into a javascript function arguments list
|
238
|
+
def to_js_args(args)
|
239
|
+
args.collect do |value|
|
240
|
+
to_js_type(value)
|
241
|
+
end.join(',')
|
242
|
+
end
|
243
|
+
|
244
|
+
# converts any ruby type into an javascript type
|
245
|
+
def to_js_type(value)
|
246
|
+
case value.class.name.to_sym
|
247
|
+
when :Float, :Fixnum, :TrueClass, :FalseClass, :Symbol then value.to_s
|
248
|
+
when :String then "\"#{@template.escape_javascript(value)}\""
|
249
|
+
when :NilClass then 'null'
|
250
|
+
when :Array then "[#{to_js_args(value)}]"
|
251
|
+
when :Proc then proc_to_function(&value)
|
252
|
+
else
|
253
|
+
|
254
|
+
# the other method-calls processing
|
255
|
+
if value.is_a?(MethodCall)
|
256
|
+
# removing the call out of the calls thread
|
257
|
+
top = value
|
258
|
+
parent = value
|
259
|
+
while parent
|
260
|
+
top = parent
|
261
|
+
parent = parent.instance_variable_get('@parent')
|
262
|
+
end
|
263
|
+
@thread.reject!{ |item| item == top }
|
264
|
+
|
265
|
+
value.to_s
|
266
|
+
|
267
|
+
# simple hashes processing
|
268
|
+
elsif value.is_a?(Hash)
|
269
|
+
pairs = []
|
270
|
+
value.each do |key, value|
|
271
|
+
pairs << "#{to_js_type(key)}:#{to_js_type(value)}"
|
272
|
+
end
|
273
|
+
"{#{pairs.sort.join(',')}}"
|
274
|
+
|
275
|
+
# JSON exportable values processing
|
276
|
+
elsif value.respond_to?(:to_json)
|
277
|
+
to_js_type(value.to_json)
|
278
|
+
|
279
|
+
# throwing an ansupported class name
|
280
|
+
else
|
281
|
+
throw "RightRails::JavaScriptGenerator doesn't instances of #{value.class.name} yet"
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
# converts a proc into a javascript function
|
287
|
+
def proc_to_function(&block)
|
288
|
+
thread = []
|
289
|
+
args = []
|
290
|
+
names = []
|
291
|
+
name = 'a'
|
292
|
+
page = RightRails::JavaScriptGenerator.new(@template, thread)
|
293
|
+
|
294
|
+
block.arity.times do |i|
|
295
|
+
args << page.get(name)
|
296
|
+
names << name
|
297
|
+
name = name.succ
|
298
|
+
end
|
299
|
+
|
300
|
+
# swapping the current thread with the block's one
|
301
|
+
old_thread = @thread
|
302
|
+
@thread = thread
|
303
|
+
|
304
|
+
yield(*args)
|
305
|
+
|
306
|
+
# swapping the current therad back
|
307
|
+
@thread = old_thread
|
308
|
+
|
309
|
+
"function(#{names.join(',')}){#{page.to_s}}"
|
310
|
+
end
|
311
|
+
end
|
312
|
+
|
313
|
+
end
|
data/lib/right_rails.rb
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../spec_helper.rb"
|
2
|
+
|
3
|
+
class DummyController
|
4
|
+
include RightRails::ControllerExtensions
|
5
|
+
|
6
|
+
attr_accessor :template
|
7
|
+
attr_accessor :request
|
8
|
+
end
|
9
|
+
|
10
|
+
describe RightRails::ControllerExtensions do
|
11
|
+
before :each do
|
12
|
+
@controller = DummyController.new
|
13
|
+
|
14
|
+
@request = mock(:request, {:content_type => Mime::JS})
|
15
|
+
|
16
|
+
@template = mock(:template, {:_evaluate_assigns_and_ivars => nil, :request => @request})
|
17
|
+
@controller.template = @template
|
18
|
+
|
19
|
+
@generator = RightRails::JavaScriptGenerator.new(@template)
|
20
|
+
RightRails::JavaScriptGenerator.stub!(:new).with(@template).and_return(@generator)
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should bypass simple calls to the JavaScriptGenerator" do
|
24
|
+
@generator.should_receive(:alert).with('that')
|
25
|
+
@generator.should_receive(:to_s).and_return('the script')
|
26
|
+
|
27
|
+
@controller.rjs.alert("that").should == {:text => 'the script', :content_type => Mime::JS}
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should process blocks properly" do
|
31
|
+
@generator.should_receive(:alert).with('boo')
|
32
|
+
@generator.should_receive(:confirm).with('foo')
|
33
|
+
|
34
|
+
@generator.should_receive(:to_s).and_return('the script')
|
35
|
+
|
36
|
+
@controller.rjs do |page|
|
37
|
+
page.alert('boo')
|
38
|
+
page.confirm('foo')
|
39
|
+
end.should == {:text => 'the script', :content_type => Mime::JS}
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should let you overload the options" do
|
43
|
+
@generator.should_receive(:alert).with('boo')
|
44
|
+
@generator.should_receive(:to_s).and_return('the script')
|
45
|
+
|
46
|
+
@controller.rjs(:content_type => Mime::HTML, :layout => 'something').alert('boo').should == {
|
47
|
+
:text => 'the script', :content_type => Mime::HTML, :layout => 'something'
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should overload content-type and layout for the iframed uploads" do
|
52
|
+
@request.should_receive(:content_type).and_return('multipart/form-data')
|
53
|
+
@generator.should_receive(:update)
|
54
|
+
@generator.should_receive(:to_s).and_return('the script')
|
55
|
+
|
56
|
+
@controller.rjs.update.should == {
|
57
|
+
:text => 'the script', :content_type => Mime::HTML, :layout => 'iframed'
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../../spec_helper.rb"
|
2
|
+
|
3
|
+
describe RightRails::Helpers::Basic do
|
4
|
+
include RightRails::Helpers::Basic
|
5
|
+
include ActionView::Helpers::TagHelper
|
6
|
+
|
7
|
+
it "should build the basic javascript include tags" do
|
8
|
+
should_receive(:javascript_include_tag).with(*%w{right right/rails})
|
9
|
+
rightjs_scripts
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should catch the optional modules" do
|
13
|
+
@_right_scripts = %w{lightbox dnd}
|
14
|
+
|
15
|
+
should_receive(:javascript_include_tag).with(*%w{right right/lightbox right/dnd right/rails})
|
16
|
+
|
17
|
+
rightjs_scripts
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should let to specify the modules as arguments" do
|
21
|
+
should_receive(:javascript_include_tag).with(*%w{right right/lightbox right/dnd right/rails})
|
22
|
+
|
23
|
+
rightjs_scripts :lightbox, :dnd
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should load internationalization modules if defined" do
|
27
|
+
should_receive(:javascript_include_tag).with(*%w{right right/rails right/i18n/ru})
|
28
|
+
|
29
|
+
I18n.locale = 'ru'
|
30
|
+
RAILS_ROOT = "rails-root"
|
31
|
+
File.should_receive(:exists?).with("rails-root/public/javascripts/right/i18n/ru.js").and_return(true)
|
32
|
+
|
33
|
+
rightjs_scripts
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not include non-existing locales" do
|
37
|
+
should_receive(:javascript_include_tag).with(*%w{right right/rails})
|
38
|
+
|
39
|
+
I18n.locale = 'some-weird-stuff'
|
40
|
+
File.should_receive(:exists?).with("rails-root/public/javascripts/right/i18n/some-weird-stuff.js").and_return(false)
|
41
|
+
|
42
|
+
rightjs_scripts
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should use the source scripts in the development environment" do
|
46
|
+
should_receive(:javascript_include_tag).with(*%w{right-src right/rails-src})
|
47
|
+
|
48
|
+
RAILS_ENV = 'development'
|
49
|
+
|
50
|
+
rightjs_scripts
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should build a script-generator for the rjs method" do
|
54
|
+
rjs.should be_a(RightRails::JavaScriptGenerator)
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should generate scripts with rjs one-liners" do
|
58
|
+
rjs.boo.boo.boo.to_s.should == 'boo().boo().boo()'
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should generate scripts with rjs blocks" do
|
62
|
+
rjs do |page|
|
63
|
+
page.boo.boo.boo
|
64
|
+
end.to_s.should == 'boo().boo().boo();'
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should generate a proper javascript tag construction" do
|
68
|
+
should_receive(:javascript_tag).and_return('javascript_tag')
|
69
|
+
|
70
|
+
rjs_tag do |page|
|
71
|
+
page.boo.boo.boo
|
72
|
+
end.to_s.should == 'javascript_tag'
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../../spec_helper.rb"
|
2
|
+
|
3
|
+
describe RightRails::Helpers::Forms do
|
4
|
+
include ActionView::Helpers::TagHelper
|
5
|
+
include ActionView::Helpers::FormTagHelper
|
6
|
+
include ActionView::Helpers::JavaScriptHelper
|
7
|
+
include RightRails::Helpers::Basic
|
8
|
+
include RightRails::Helpers::Forms
|
9
|
+
|
10
|
+
def url_for(options) options end
|
11
|
+
def escape_javascript(str) str end
|
12
|
+
|
13
|
+
it "should generate a #calendar_field_tag" do
|
14
|
+
calendar_field_tag('name', 'value').should ==
|
15
|
+
%Q{<input id="name" name="name" rel="calendar" type="text" value="value" />}
|
16
|
+
|
17
|
+
@_right_scripts.should == ['calendar']
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should generate a #calendar_field_tag with options" do
|
21
|
+
calendar_field_tag('name', 'value', :format => '%Y/%m/%d').should ==
|
22
|
+
%Q{<input data-calendar-options="{format:'%Y/%m/%d'}" id="name" name="name" rel="calendar" type="text" value="value" />}
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should generate a #autocomplete_field_tag" do
|
26
|
+
autocomplete_field_tag('name', 'value', :url => '/foo').should ==
|
27
|
+
%Q{<input autocomplete="off" id="name" name="name" rel="autocompleter[/foo]" type="text" value="value" />}
|
28
|
+
|
29
|
+
@_right_scripts.should == ['autocompleter']
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should generate a #autocomplete_field_tag with options" do
|
33
|
+
autocomplete_field_tag('name', 'value', :url => '/foo', :spinner => 'spinner', :min_length => 2).should ==
|
34
|
+
%Q{<input autocomplete="off" data-autocompleter-options="{minLength:2,spinner:'spinner'}" id="name" name="name" rel="autocompleter[/foo]" type="text" value="value" />}
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should generate a #slider_tag with options" do
|
38
|
+
slider_tag('some_field', 22, :min => 10, :max => 40).should ==
|
39
|
+
%Q{<input id="some_field" name="some_field" type="hidden" value="22" />\n<script type="text/javascript">\n//<![CDATA[\nnew Slider({max:40,min:10,value:22}).insertTo('some_field','after').assignTo('some_field');\n//]]>\n</script>}
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should generate a #rater_tag" do
|
43
|
+
rater_tag('some_field', 2).should ==
|
44
|
+
%Q{<input id="some_field" name="some_field" type="hidden" value="2" />\n<script type="text/javascript">\n//<![CDATA[\nnew Rater({value:2}).insertTo('some_field','after').assignTo('some_field');\n//]]>\n</script>}
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should generate the #rater_display tag" do
|
48
|
+
rater_display(4).should ==
|
49
|
+
%Q{<div class="right-rater right-rater-disabled"><div class="right-rater-glow">★</div><div class="right-rater-glow">★</div><div class="right-rater-glow">★</div><div class="right-rater-glow">★</div><div>★</div></div>}
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require File.dirname(__FILE__) + "/../../../spec_helper.rb"
|
2
|
+
|
3
|
+
describe RightRails::Helpers::Misc do
|
4
|
+
include ActionView::Helpers::UrlHelper
|
5
|
+
include ActionView::Helpers::TagHelper
|
6
|
+
include ActionView::Helpers::TextHelper
|
7
|
+
include RightRails::Helpers::Basic
|
8
|
+
include RightRails::Helpers::Misc
|
9
|
+
|
10
|
+
it "should provide the basic #flashes builder" do
|
11
|
+
should_receive(:flash).any_number_of_times.and_return({
|
12
|
+
:warning => "Warning!",
|
13
|
+
:notice => "Notice!",
|
14
|
+
:error => "Error!"
|
15
|
+
})
|
16
|
+
|
17
|
+
flashes.should == '<div id="flashes">'+
|
18
|
+
'<div class="error">Error!</div>'+
|
19
|
+
'<div class="notice">Notice!</div>'+
|
20
|
+
'<div class="warning">Warning!</div>'+
|
21
|
+
'</div>'
|
22
|
+
end
|
23
|
+
|
24
|
+
describe "#autocomplete_result" do
|
25
|
+
it "should generate a simple result" do
|
26
|
+
autocomplete_result(%w{one two three}).should == '<ul><li>one</li><li>two</li><li>three</li></ul>'
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should generate result with highlightning" do
|
30
|
+
autocomplete_result(%w{one two three}, :highlight => 'o').should ==
|
31
|
+
'<ul><li><strong class="highlight">o</strong>ne</li><li>tw<strong class="highlight">o</strong></li><li>three</li></ul>'
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should escape strings by default" do
|
35
|
+
autocomplete_result(['<b>b</b>', '<i>i</i>']).should ==
|
36
|
+
%Q{<ul><li><b>b</b></li><li><i>i</i></li></ul>}
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should not escape strings if asked" do
|
40
|
+
autocomplete_result(['<b>b</b>', '<i>i</i>'], :escape => false).should ==
|
41
|
+
%Q{<ul><li><b>b</b></li><li><i>i</i></li></ul>}
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should generate result out of list of records" do
|
45
|
+
records = [
|
46
|
+
mock(:boo, :boo => 'one'),
|
47
|
+
mock(:boo, :boo => 'two')
|
48
|
+
]
|
49
|
+
|
50
|
+
autocomplete_result(records, :boo).should == '<ul><li>one</li><li>two</li></ul>'
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should highlight result when generated out of an objects list" do
|
54
|
+
records = [
|
55
|
+
mock(:boo, :boo => 'one'),
|
56
|
+
mock(:boo, :boo => 'two')
|
57
|
+
]
|
58
|
+
|
59
|
+
autocomplete_result(records, :boo, :highlight => 'o').should ==
|
60
|
+
%Q{<ul><li><strong class="highlight">o</strong>ne</li><li>tw<strong class="highlight">o</strong></li></ul>}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "#link_to_lightbox" do
|
65
|
+
it "should generate the link" do
|
66
|
+
link_to_lightbox('boo', 'boo').should == '<a href="boo" rel="lightbox">boo</a>'
|
67
|
+
@_right_scripts.should == ['lightbox']
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should generate lightbox with roadtrip" do
|
71
|
+
link_to_lightbox('boo', 'boo', :roadtrip => true).should ==
|
72
|
+
%Q{<a href="boo" rel="lightbox[roadtrip]">boo</a>}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "#tabs generator" do
|
77
|
+
def capture(&block)
|
78
|
+
return yield()
|
79
|
+
end
|
80
|
+
|
81
|
+
def concat(content)
|
82
|
+
content
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should generate simple tabs" do
|
86
|
+
tabs(:id => 'my-tabs') {
|
87
|
+
tab("Tab 1", :id => 'tab-1'){ 'content 1' }
|
88
|
+
tab("Tab 2", :id => 'tab-2'){ 'content 2' }
|
89
|
+
}.should == %Q{<ul id=\"my-tabs\"><ul><li><a href=\"#tab-1\">Tab 1</a></li>\n<li><a href=\"#tab-2\">Tab 2</a></li></ul>\n<li id=\"tab-1\">content 1</li>\n<li id=\"tab-2\">content 2</li>\n</ul>\n<script type=\"text/javascript\">\n//<![CDATA[\nnew Tabs('my-tabs');\n//]]>\n</script>}
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should generate tabs with id prefix" do
|
93
|
+
tabs(:id => 'my-tabs', :id_prefix => 'foo-') {
|
94
|
+
tab("Tab 1", :id => 'tab-1'){ 'content 1' }
|
95
|
+
tab("Tab 2", :id => 'tab-2'){ 'content 2' }
|
96
|
+
}.should == %Q{<ul data-tabs-options=\"{idPrefix:'foo-'}\" id=\"my-tabs\"><ul><li><a href=\"#tab-1\">Tab 1</a></li>\n<li><a href=\"#tab-2\">Tab 2</a></li></ul>\n<li id=\"foo-tab-1\">content 1</li>\n<li id=\"foo-tab-2\">content 2</li>\n</ul>\n<script type=\"text/javascript\">\n//<![CDATA[\nnew Tabs('my-tabs');\n//]]>\n</script>}
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should generate remote tabs" do
|
100
|
+
tabs(:id => 'my-tabs') {
|
101
|
+
tab("Tab 1", :url => '/tab/1')
|
102
|
+
tab("Tab 2", :url => '/tab/2')
|
103
|
+
}.should == %Q{<ul id=\"my-tabs\"><ul><li><a href=\"/tab/1\">Tab 1</a></li>\n<li><a href=\"/tab/2\">Tab 2</a></li></ul>\n</ul>\n<script type=\"text/javascript\">\n//<![CDATA[\nnew Tabs('my-tabs');\n//]]>\n</script>}
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should generate carousel tabs" do
|
107
|
+
tabs(:id => 'my-tabs', :type => :carousel) {
|
108
|
+
tab("Tab 1", :id => 'tab-1'){ 'content 1' }
|
109
|
+
tab("Tab 2", :id => 'tab-2'){ 'content 2' }
|
110
|
+
}.should == %Q{<ul class=\"right-tabs-carousel\" id=\"my-tabs\"><ul><li><a href=\"#tab-1\">Tab 1</a></li>\n<li><a href=\"#tab-2\">Tab 2</a></li></ul>\n<li id=\"tab-1\">content 1</li>\n<li id=\"tab-2\">content 2</li>\n</ul>\n<script type=\"text/javascript\">\n//<![CDATA[\nnew Tabs('my-tabs');\n//]]>\n</script>}
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should generate harmonica tabs" do
|
114
|
+
tabs(:id => 'my-tabs', :type => :harmonica) {
|
115
|
+
tab("Tab 1", :id => 'tab-1'){ 'content 1' }
|
116
|
+
tab("Tab 2", :id => 'tab-2'){ 'content 2' }
|
117
|
+
}.should == %Q{<dl id=\"my-tabs\"><dt><a href=\"#tab-1\">Tab 1</a></dt>\n<dd id=\"tab-1\">content 1</dd>\n<dt><a href=\"#tab-2\">Tab 2</a></dt>\n<dd id=\"tab-2\">content 2</dd></dl>\n<script type=\"text/javascript\">\n//<![CDATA[\nnew Tabs('my-tabs');\n//]]>\n</script>}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|