right-rails 1.0.12 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ 2011-03-01 Nikolay Nemshilov
2
+ o Version 1.1.0
3
+ * RightJS CDN Server support
4
+ * RightJS 2.2.2 builds
5
+
1
6
  2010-10-24 Nikolay Nemshilov
2
7
  o Version 1.0.10
3
8
  * RightJS builds update
data/Rakefile CHANGED
@@ -1,34 +1,26 @@
1
1
  require 'rake'
2
- require 'rake/testtask'
3
2
  require 'rake/rdoctask'
4
- require 'spec/rake/spectask'
3
+ require 'rspec/core/rake_task'
5
4
 
6
5
  desc 'Default: run rspec tests.'
7
6
  task :default => :spec
8
7
 
9
8
  desc "Run all specs in spec directory"
10
- Spec::Rake::SpecTask.new(:spec) do |t|
11
- t.spec_opts = ['--options', "spec/spec.opts"]
12
- t.spec_files = FileList['spec/**/*_spec.rb']
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ t.rspec_opts = ['--options', "spec/spec.opts"]
11
+ t.pattern = 'spec/**/*_spec.rb'
13
12
  end
14
13
 
15
14
  namespace :spec do
16
15
  desc "Run all specs in spec directory with RCov"
17
- Spec::Rake::SpecTask.new(:rcov) do |t|
18
- t.spec_opts = ['--options', "spec/spec.opts"]
19
- t.spec_files = FileList['spec/**/*_spec.rb']
20
- t.rcov = true
21
- t.rcov_dir = 'coverage'
22
- t.rcov_opts = lambda do
16
+ RSpec::Core::RakeTask.new(:rcov) do |t|
17
+ t.rspec_opts = ['--options', "spec/spec.opts"]
18
+ t.pattern = 'spec/**/*_spec.rb'
19
+ t.rcov = true
20
+ t.rcov_opts = lambda do
23
21
  IO.readlines("spec/rcov.opts").map {|l| l.chomp.split " "}.flatten
24
22
  end
25
23
  end
26
-
27
- # desc "Print Specdoc for all specs"
28
- # Spec::Rake::SpecTask.new(:doc) do |t|
29
- # t.spec_opts = ["--format", "specdoc", "--dry-run"]
30
- # t.spec_files = FileList['spec/**/*_spec.rb']
31
- # end
32
24
  end
33
25
 
34
26
  desc 'Generate documentation for the right_rails plugin.'
@@ -11,23 +11,27 @@
11
11
  # - RightRails::Config.include_rails_module +true+ or +false+
12
12
  # - RightRails::Config.swap_builds_and_sources +true+ or +false+
13
13
  # - RightRails::Config.include_scripts_automatically +true+ or +false+
14
+ # - RightRails::Config.use_cdn_in_production +false+ or +true+
15
+ # - RigthRails::Config.cdn_url 'http://cdn.rightjs.org'
14
16
  #
15
17
  # When you set a property in _auto_ then the script will try to figure the
16
18
  # parameters out by the current environment and the content of the
17
19
  # public/javascript/right.js file
18
20
  #
19
21
  module RightRails::Config
20
-
22
+
21
23
  DEFAULT_PUBLIC_PATH = 'auto' # 'auto' or some path
22
24
  DEFAULT_SAFE_MODE = 'auto' # 'auto', true or false
23
25
  DEFAULT_RIGHTJS_VERSION = 'auto' # 'auto', 2 or 1
24
26
  DEFAULT_ENVIRONMENT = 'auto' # 'auto', 'production' or 'development'
25
27
  DEFAULT_RIGHTJS_LOCATION = 'javascripts/right.js'
26
28
  DEFAULT_LOCALES_LOCATION = 'javascripts/right/i18n'
29
+ DEFAULT_CDN_URL = 'http://cdn.rightjs.org'
30
+ DEFAULT_USE_CDN = false # false or true
27
31
  DEFAULT_INCLUDE_RAILS = true # true or false
28
32
  DEFAULT_AUTO_INCLUDES = true # true or false
29
33
  DEFAULT_SWAP_BUILDS = true # true or false
30
-
34
+
31
35
  class << self
32
36
  #
33
37
  # Resetting the configuration to the defaults
@@ -41,8 +45,10 @@ module RightRails::Config
41
45
  remove_instance_variable(:@include_rails) if defined?(@include_rails)
42
46
  remove_instance_variable(:@auto_includes) if defined?(@auto_includes)
43
47
  remove_instance_variable(:@swap_builds) if defined?(@swap_builds)
48
+ remove_instance_variable(:@use_cdn) if defined?(@use_cdn)
49
+ remove_instance_variable(:@cdn_url) if defined?(@cdn_url)
44
50
  end
45
-
51
+
46
52
  #
47
53
  # Returns the currently used environment
48
54
  #
@@ -52,10 +58,10 @@ module RightRails::Config
52
58
  unless defined?(@environment)
53
59
  self.env = DEFAULT_ENVIRONMENT
54
60
  end
55
-
61
+
56
62
  @environment
57
63
  end
58
-
64
+
59
65
  #
60
66
  # Sets the current environment, which will effectively make
61
67
  # RightRails to switch between the builds and src versions of
@@ -71,17 +77,17 @@ module RightRails::Config
71
77
  "production"
72
78
  end
73
79
  end
74
-
80
+
75
81
  @environment = value.to_s == 'production' ? 'production' : 'development'
76
82
  end
77
-
83
+
78
84
  #
79
85
  # Returns a marker if we are in the _development_ environment
80
86
  #
81
87
  def dev_env?
82
88
  env == 'development'
83
89
  end
84
-
90
+
85
91
  #
86
92
  # Returns the public_html directory path
87
93
  #
@@ -91,10 +97,10 @@ module RightRails::Config
91
97
  unless defined?(@public_path)
92
98
  self.public_path = DEFAULT_PUBLIC_PATH
93
99
  end
94
-
100
+
95
101
  @public_path
96
102
  end
97
-
103
+
98
104
  #
99
105
  # If you have your public_html somewhere else
100
106
  # this is the place where you can define it
@@ -108,15 +114,15 @@ module RightRails::Config
108
114
  "public"
109
115
  end
110
116
  end
111
-
117
+
112
118
  # getting rid of the trailing slash
113
119
  if path.slice(path.size-1, path.size) == '/'
114
- path = path.slice(0, path.size-1)
120
+ path = path.slice(0, path.size-1)
115
121
  end
116
-
122
+
117
123
  @public_path = path.dup
118
124
  end
119
-
125
+
120
126
  #
121
127
  # Returns a full-path to the localization modules directory
122
128
  #
@@ -124,22 +130,22 @@ module RightRails::Config
124
130
  unless defined?(@locales_path)
125
131
  self.locales_path = "#{public_path}/#{DEFAULT_LOCALES_LOCATION}"
126
132
  end
127
-
133
+
128
134
  @locales_path
129
135
  end
130
-
136
+
131
137
  #
132
138
  # Sets the RightJS localilzation modules directory path
133
139
  #
134
140
  def locales_path=(path)
135
141
  # getting rid of the trailing slash
136
142
  if path.slice(path.size-1, path.size) == '/'
137
- path = path.slice(0, path.size-1)
143
+ path = path.slice(0, path.size-1)
138
144
  end
139
-
145
+
140
146
  @locales_path = path
141
147
  end
142
-
148
+
143
149
  #
144
150
  # Checks if the RightJS is used in the safe-mode
145
151
  #
@@ -147,10 +153,10 @@ module RightRails::Config
147
153
  unless defined?(@safe_mode)
148
154
  self.safe_mode = DEFAULT_SAFE_MODE
149
155
  end
150
-
156
+
151
157
  @safe_mode
152
158
  end
153
-
159
+
154
160
  #
155
161
  # Setup whether RightJS is used in the safe-mode or not
156
162
  #
@@ -161,7 +167,7 @@ module RightRails::Config
161
167
  def safe_mode=(value)
162
168
  @safe_mode = !!(value == 'auto' ? read_rightjs_file =~ /\.safe\s*=\s*true/ : value)
163
169
  end
164
-
170
+
165
171
  #
166
172
  # Returns the RightJS version (2 or 1)
167
173
  #
@@ -169,10 +175,10 @@ module RightRails::Config
169
175
  unless defined?(@rightjs_version)
170
176
  self.rightjs_version = DEFAULT_RIGHTJS_VERSION
171
177
  end
172
-
178
+
173
179
  @rightjs_version
174
180
  end
175
-
181
+
176
182
  #
177
183
  # With this method you can set up which version of RightJS do you use
178
184
  # 2 or 1. You also can set it to a string 'auto' (by default) in which
@@ -183,10 +189,10 @@ module RightRails::Config
183
189
  if value == 'auto'
184
190
  value = read_rightjs_file =~ /version\s*(:|=)\s*("|')1/ ? 1 : 2
185
191
  end
186
-
192
+
187
193
  @rightjs_version = value < 2 ? 1 : 2
188
194
  end
189
-
195
+
190
196
  #
191
197
  # Returns a marker if the script should automatically include
192
198
  # the ruby-on-rails module for RightJS
@@ -195,10 +201,10 @@ module RightRails::Config
195
201
  unless defined?(@include_rails)
196
202
  self.include_rails_module = DEFAULT_INCLUDE_RAILS
197
203
  end
198
-
204
+
199
205
  @include_rails
200
206
  end
201
-
207
+
202
208
  #
203
209
  # Sets the marker if the plugin should automatically include
204
210
  # the ruby-on-rails module for RightJS
@@ -206,7 +212,7 @@ module RightRails::Config
206
212
  def include_rails_module=(value)
207
213
  @include_rails = !! value
208
214
  end
209
-
215
+
210
216
  #
211
217
  # Checks if the plugin should automatically swap between
212
218
  # the builds and the source-versions of JavaScript files
@@ -216,10 +222,10 @@ module RightRails::Config
216
222
  unless defined?(@swap_builds)
217
223
  self.swap_builds_and_sources = DEFAULT_SWAP_BUILDS
218
224
  end
219
-
225
+
220
226
  @swap_builds
221
227
  end
222
-
228
+
223
229
  #
224
230
  # Sets the marker if the plugin should automatically
225
231
  # swap between source and builds of JavaScript files
@@ -227,7 +233,7 @@ module RightRails::Config
227
233
  def swap_builds_and_sources=(value)
228
234
  @swap_builds = !! value
229
235
  end
230
-
236
+
231
237
  #
232
238
  # Checks if the plugin should include JavaScript modules
233
239
  # automatically when they needed
@@ -236,10 +242,10 @@ module RightRails::Config
236
242
  unless defined?(@auto_includes)
237
243
  self.include_scripts_automatically = DEFAULT_AUTO_INCLUDES
238
244
  end
239
-
245
+
240
246
  @auto_includes
241
247
  end
242
-
248
+
243
249
  #
244
250
  # Sets the marker if the plugin should automatically include
245
251
  # all the JavaScript files on the page when they needed
@@ -247,15 +253,55 @@ module RightRails::Config
247
253
  def include_scripts_automatically=(value)
248
254
  @auto_includes = !! value
249
255
  end
250
-
256
+
257
+ #
258
+ # Checks if the plugin going to automatically use
259
+ # CDN Server in production instead of local files
260
+ #
261
+ def use_cdn_in_production
262
+ unless defined?(@use_cdn)
263
+ self.use_cdn_in_production = DEFAULT_USE_CDN
264
+ end
265
+
266
+ @use_cdn
267
+ end
268
+
269
+ #
270
+ # Sets the marker if the plugin should automatically use
271
+ # CDN Server in production instead of the local files
272
+ #
273
+ def use_cdn_in_production=(value)
274
+ @use_cdn = !! value
275
+ end
276
+
277
+ #
278
+ # Returns the current CDN Server URL address
279
+ #
280
+ def cdn_url
281
+ unless defined?(@cdn_url)
282
+ self.cdn_url = DEFAULT_CDN_URL
283
+ end
284
+
285
+ @cdn_url
286
+ end
287
+
288
+ #
289
+ # Sets a new CDN Server url address
290
+ #
291
+ def cdn_url=(value)
292
+ @cdn_url = value
293
+ end
294
+
295
+
251
296
  # boolean methods aliases
252
297
  alias_method :safe_mode?, :safe_mode
253
298
  alias_method :include_rails_module? , :include_rails_module
299
+ alias_method :use_cdn_in_production?, :use_cdn_in_production
254
300
  alias_method :swap_builds_and_sources? , :swap_builds_and_sources
255
301
  alias_method :include_scripts_automatically?, :include_scripts_automatically
256
-
302
+
257
303
  private
258
-
304
+
259
305
  #
260
306
  # reading the `public/javascripts/right.js` file for the purposes
261
307
  # of the automatic configuration
@@ -267,7 +313,7 @@ module RightRails::Config
267
313
  rescue
268
314
  ''
269
315
  end
270
-
316
+
271
317
  #
272
318
  # Checking if we are in the Ruby-on-Rails environment
273
319
  #
@@ -9,20 +9,20 @@ module RightRails::Helpers::Rails
9
9
  #
10
10
  def javascript_include_tag(first, *others)
11
11
  if (first == :defaults)
12
- options = others.last.is_a?(Hash) ? modules.pop : nil
12
+ options = others.last.is_a?(Hash) ? others.pop : nil
13
13
 
14
14
  rightjs_require_module *others
15
15
 
16
16
  scripts = RightRails::Helpers.required_js_files(self)
17
17
  scripts << 'application'
18
18
  scripts << options unless options.nil?
19
-
20
- super scripts
19
+
20
+ super *scripts
21
21
  else
22
22
  super first, *others
23
23
  end
24
24
  end
25
-
25
+
26
26
  #
27
27
  # Overloading the `remote_function` helper so it used the RightJS semantics
28
28
  #
@@ -31,18 +31,18 @@ module RightRails::Helpers::Rails
31
31
  url = options[:url]
32
32
  url = url.merge(:escape => false) if url.is_a?(Hash)
33
33
  options[:url] = escape_javascript(url_for(url))
34
-
34
+
35
35
  cmd = RightRails::Helpers.build_xhr_request(options)
36
-
36
+
37
37
  cmd = "#{options[:before]};#{cmd}" if options[:before]
38
38
  cmd << ";#{options[:after]}" if options[:after]
39
-
39
+
40
40
  cmd = "if(#{options[:condition]}){#{cmd}}" if options[:condition]
41
41
  cmd = "if(confirm('#{escape_javascript(options[:confirm])}')){#{cmd}}" if options[:confirm]
42
-
42
+
43
43
  cmd
44
44
  end
45
-
45
+
46
46
  #
47
47
  # Overloading the `submit_to_remote` so it used RightJS instead
48
48
  #
@@ -54,7 +54,7 @@ module RightRails::Helpers::Rails
54
54
 
55
55
  button_to_remote(value, options, html_options)
56
56
  end
57
-
57
+
58
58
  #
59
59
  # Replacing `periodically_call_remote` method
60
60
  #
@@ -63,41 +63,41 @@ module RightRails::Helpers::Rails
63
63
  code = "function(){#{remote_function(options)}}.periodical(#{frequency * 1000})"
64
64
  javascript_tag(code)
65
65
  end
66
-
66
+
67
67
  #
68
68
  # replacing the draggables generator to make our autoscripts stuff working
69
69
  #
70
70
  def draggable_element_js(*args)
71
71
  rightjs_require_module 'dnd'
72
-
72
+
73
73
  super *args
74
74
  end
75
-
75
+
76
76
  #
77
77
  # replace the droppables generator to be used with RightJS
78
78
  #
79
79
  def drop_receiving_element_js(*args)
80
80
  rightjs_require_module 'dnd'
81
-
81
+
82
82
  super(*args).gsub!('Droppables.add', 'new Droppable'
83
83
  ).gsub!('element.id', 'draggable.element.id'
84
84
  ).gsub!('(element)', '(draggable)')
85
85
  end
86
-
86
+
87
87
  #
88
88
  # catching the sortables generator
89
89
  #
90
90
  def sortable_element_js(id, options={})
91
91
  rightjs_require_module 'dnd', 'sortable'
92
-
92
+
93
93
  script = "new Sortable('#{id}'"
94
-
94
+
95
95
  # processing the options
96
96
  options[:url] = escape_javascript(url_for(options[:url])) if options[:url]
97
97
  s_options = RightRails::Helpers.unit_options(options, 'sortable')
98
98
  script << ",#{s_options}" unless s_options == '{}'
99
-
99
+
100
100
  script << ")"
101
101
  end
102
-
102
+
103
103
  end
@@ -2,12 +2,12 @@
2
2
  # There is the namespace for the helpers and some private methods
3
3
  #
4
4
  module RightRails::Helpers
5
-
5
+
6
6
  autoload :Basic, 'right_rails/helpers/basic'
7
7
  autoload :Rails, 'right_rails/helpers/rails'
8
8
  autoload :Forms, 'right_rails/helpers/forms'
9
9
  autoload :Misc, 'right_rails/helpers/misc'
10
-
10
+
11
11
  ##########################################################################
12
12
  # Varios RightJS unit keys
13
13
  ##########################################################################
@@ -24,7 +24,7 @@ module RightRails::Helpers
24
24
  spinnerFx
25
25
  params
26
26
  }
27
-
27
+
28
28
  CALENDAR_OPTION_KEYS = %w{
29
29
  format
30
30
  showTime
@@ -86,7 +86,7 @@ module RightRails::Helpers
86
86
  fxName
87
87
  fxDuration
88
88
  }
89
-
89
+
90
90
  TABS_OPTION_KEYS = %w{
91
91
  idPrefix
92
92
  tabsElement
@@ -104,7 +104,7 @@ module RightRails::Helpers
104
104
  Xhr
105
105
  Cookie
106
106
  }
107
-
107
+
108
108
  LIGHTBOX_OPTION_KEYS = %w{
109
109
  group
110
110
  endOpacity
@@ -116,14 +116,14 @@ module RightRails::Helpers
116
116
  mediaWidth
117
117
  mediaHeight
118
118
  }
119
-
119
+
120
120
  RESIZABLE_OPTION_KEYS = %w{
121
121
  minWidth
122
122
  maxWidth
123
123
  minHeight
124
124
  maxHeight
125
125
  }
126
-
126
+
127
127
  SORTABLE_OPTION_KEYS = %w{
128
128
  url
129
129
  direction
@@ -137,7 +137,7 @@ module RightRails::Helpers
137
137
  accept
138
138
  minLength
139
139
  }
140
-
140
+
141
141
  protected
142
142
 
143
143
  class << self
@@ -147,7 +147,7 @@ protected
147
147
  def prefix
148
148
  RightRails::Config.safe_mode? ? 'RightJS.' : ''
149
149
  end
150
-
150
+
151
151
  #
152
152
  # Switches between the css class-names prefixes
153
153
  # depending on current RightJS version
@@ -155,7 +155,7 @@ protected
155
155
  def css_prefix
156
156
  RightRails::Config.rightjs_version < 2 ? 'right' : 'rui'
157
157
  end
158
-
158
+
159
159
  #
160
160
  # Converting the string into an html-safe eqivalent
161
161
  # regardless of the current environment
@@ -169,7 +169,7 @@ protected
169
169
  string
170
170
  end
171
171
  end
172
-
172
+
173
173
  #
174
174
  # Tells the script that the user needs this module
175
175
  #
@@ -177,50 +177,71 @@ protected
177
177
  #
178
178
  def require_js_module(context, *list)
179
179
  registry = modules_registry_for(context)
180
-
180
+
181
181
  list.each do |name|
182
182
  unless registry.include?(name.to_s)
183
183
  registry << name.to_s
184
184
  end
185
185
  end
186
186
  end
187
-
187
+
188
188
  #
189
189
  # Returns a list of required JavaScript files for RightJS
190
190
  #
191
191
  def required_js_files(context)
192
192
  scripts = ['right']
193
-
194
-
195
- if RightRails::Config.include_scripts_automatically?
193
+ config = RightRails::Config
194
+
195
+
196
+ if config.include_scripts_automatically?
196
197
  # hooking up the 'rails' javascript module if required
197
- scripts << 'right/rails' if RightRails::Config.include_rails_module?
198
-
198
+ scripts << 'right/rails' if config.include_rails_module?
199
+
199
200
  # adding the modules if needed
200
201
  scripts += modules_registry_for(context).collect do |package|
201
202
  "right/#{package}"
202
203
  end
203
-
204
+
204
205
  # swapping to the sources in the development mode
205
- if RightRails::Config.swap_builds_and_sources? && RightRails::Config.dev_env?
206
+ if config.swap_builds_and_sources? && config.dev_env?
206
207
  scripts = scripts.collect do |package|
207
208
  "#{package}-src"
208
209
  end
209
210
  end
210
-
211
+
211
212
  # loading up the locales if available
212
213
  if defined?(I18n)
213
- locale_file = "#{RightRails::Config.locales_path}/#{I18n.locale.to_s.downcase}"
214
+ locale_file = "#{config.locales_path}/#{I18n.locale.to_s.downcase}"
214
215
 
215
216
  if File.exists? "#{locale_file}.js"
216
- scripts << locale_file.slice(RightRails::Config.public_path.size + "/javascript/".size + 1, locale_file.size)
217
+ scripts << locale_file.slice(config.public_path.size + "/javascripts/".size, locale_file.size)
217
218
  end
218
219
  end
219
220
  end
220
-
221
+
222
+ # switching to CDN server if asked
223
+ if !config.dev_env? && config.use_cdn_in_production?
224
+ scripts.map! do |script|
225
+ header = File.read("#{config.public_path}/javascripts/#{script}.js", 100)
226
+
227
+ if version = header[/\d+\.\d+\.\d+/]
228
+ script += "-#{version}"
229
+ end
230
+
231
+ if script.slice(0, 6) == 'right/' # plugins and modules
232
+ script.gsub! 'right/', (
233
+ header.include?('/ui/') ? 'ui/' : 'plugins/'
234
+ )
235
+ script.gsub! 'plugins/', '' if script.include?('/i18n/')
236
+ end
237
+
238
+ "#{config.cdn_url}/#{script}.js"
239
+ end
240
+ end
241
+
221
242
  scripts
222
243
  end
223
-
244
+
224
245
  #
225
246
  # Returns the rightjs-modules registry for the context
226
247
  #
@@ -229,7 +250,7 @@ protected
229
250
  return @___rightjs_modules_registry ||= []
230
251
  end
231
252
  end
232
-
253
+
233
254
  #
234
255
  # Collects the RightJS unit options out of the given list of options
235
256
  #
@@ -261,22 +282,22 @@ protected
261
282
 
262
283
  "{#{unit_options.sort.join(',')}}"
263
284
  end
264
-
285
+
265
286
  #
266
287
  # Adds the unit options to the options list
267
288
  #
268
289
  def add_unit_options(options, unit)
269
290
  options_string = unit_options(options, unit)
270
-
291
+
271
292
  if RightRails::Config.rightjs_version > 1
272
293
  options["data-#{unit}"] = options_string
273
294
  elsif options_string != '{}'
274
- options["data-#{unit}-options"] = options_string
295
+ options["data-#{unit}-options"] = options_string
275
296
  end
276
-
297
+
277
298
  options
278
299
  end
279
-
300
+
280
301
  #
281
302
  # Removes the unit option keys out of the given options
282
303
  #
@@ -284,12 +305,12 @@ protected
284
305
  unit_keys = get_keys_for(unit)
285
306
  options.reject{ |k, v| unit_keys.include?(k.to_s) }
286
307
  end
287
-
308
+
288
309
  # returns a list of keys for the unit
289
310
  def get_keys_for(unit)
290
311
  const_get("#{unit.to_s.upcase}_OPTION_KEYS") || []
291
312
  end
292
-
313
+
293
314
  #
294
315
  # Builds a RightJS based Xhr request call
295
316
  #
@@ -297,9 +318,9 @@ protected
297
318
  xhr = options[:submit] ?
298
319
  "new #{RightRails::Helpers.prefix}Xhr(" :
299
320
  "#{RightRails::Helpers.prefix}Xhr.load("
300
-
321
+
301
322
  xhr << "'#{options[:url]}'"
302
-
323
+
303
324
  # building the options
304
325
  xhr_options = { :onSuccess => '', :onFailure => '', :onComplete => '' }
305
326
 
@@ -362,7 +383,7 @@ protected
362
383
 
363
384
  xhr
364
385
  end
365
-
386
+
366
387
  end
367
-
388
+
368
389
  end
data/lib/right_rails.rb CHANGED
@@ -2,7 +2,7 @@
2
2
  # The RightRails module. Just a namespace
3
3
  #
4
4
  module RightRails
5
- VERSION = '1.0.11'
5
+ VERSION = '1.1.0'
6
6
 
7
7
  autoload :Config, 'right_rails/config'
8
8
  autoload :JavaScriptGenerator, 'right_rails/java_script_generator'
@@ -5,54 +5,62 @@ describe RightRails::Config do
5
5
  @config = RightRails::Config
6
6
  @config.reset!
7
7
  end
8
-
8
+
9
9
  describe "by default" do
10
10
  it "should use the Rails.env by default" do
11
11
  Rails.should_receive(:env).and_return('production')
12
12
  @config.env.should == 'production'
13
13
  end
14
-
14
+
15
15
  it "should use the Rails.public_path" do
16
16
  Rails.should_receive(:public_path).and_return("/rails/public")
17
17
  @config.public_path.should == "/rails/public"
18
18
  end
19
-
19
+
20
20
  it "should return correct locales_path" do
21
21
  @config.locales_path.should == "#{@config.public_path}/#{@config::DEFAULT_LOCALES_LOCATION}"
22
22
  end
23
-
23
+
24
24
  it "should be in the normal mode" do
25
25
  @config.safe_mode.should == false
26
26
  end
27
-
27
+
28
28
  it "should be in RightJS 2 mode" do
29
29
  @config.rightjs_version.should == 2
30
30
  end
31
-
31
+
32
32
  it "should require rails module" do
33
33
  @config.include_rails_module.should == true
34
34
  end
35
-
35
+
36
36
  it "should swap builds and sources" do
37
37
  @config.swap_builds_and_sources.should == true
38
38
  end
39
-
39
+
40
40
  it "should include scripts automatically" do
41
41
  @config.include_scripts_automatically.should == true
42
42
  end
43
-
43
+
44
+ it "should not use CDN server in production" do
45
+ @config.use_cdn_in_production.should == false
46
+ end
47
+
48
+ it "should refer to the default RightJS CDN Server" do
49
+ @config.cdn_url.should == @config::DEFAULT_CDN_URL
50
+ end
51
+
44
52
  describe "with non-standard right.js" do
45
53
  before :each do
46
54
  rightjs_source_mock = %Q{
47
55
  RightJS={version: "1.5.6"};
48
56
  RightJS.safe = true;
49
57
  }
50
-
58
+
51
59
  File.should_receive(:read).with(
52
60
  "#{@config.public_path}/#{@config::DEFAULT_RIGHTJS_LOCATION}"
53
61
  ).and_return(rightjs_source_mock)
54
62
  end
55
-
63
+
56
64
  it "should be in safe-mode" do
57
65
  @config.safe_mode.should == true
58
66
  end
@@ -61,70 +69,80 @@ describe RightRails::Config do
61
69
  @config.rightjs_version.should == 1
62
70
  end
63
71
  end
64
-
72
+
65
73
  describe "without Rails" do
66
- before :each do
74
+ before :each do
67
75
  Object.should_receive(:const_get).with(:Rails).and_return(nil)
68
76
  end
69
-
77
+
70
78
  it "should fall in 'production' env" do
71
79
  @config.env.should == 'production'
72
80
  end
73
-
81
+
74
82
  it "should use a different path when there is no Rails" do
75
83
  @config.public_path.should == "public"
76
84
  end
77
85
  end
78
86
  end
79
-
87
+
80
88
  describe "with custom settings" do
81
89
  it "should be in a development environment when told" do
82
90
  @config.env = 'development'
83
91
  @config.env.should == 'development'
84
92
  end
85
-
93
+
86
94
  it "should allow to change the public_path" do
87
95
  @config.public_path = 'some/place/'
88
96
  @config.public_path.should == 'some/place'
89
97
  end
90
-
98
+
91
99
  it "should allow to change the locales location" do
92
100
  @config.locales_path = "boo/hoo"
93
101
  @config.locales_path.should == 'boo/hoo'
94
102
  end
95
-
103
+
96
104
  it "should be in safe mode when said so" do
97
105
  @config.safe_mode = true
98
106
  @config.safe_mode.should == true
99
107
  end
100
-
108
+
101
109
  it "should remain in RightJS 1 mode if said so" do
102
110
  @config.rightjs_version = 1
103
111
  @config.rightjs_version.should == 1
104
112
  end
105
-
113
+
106
114
  it "should not include rails module if asked" do
107
115
  @config.include_rails_module = false
108
116
  @config.include_rails_module.should == false
109
117
  end
110
-
118
+
111
119
  it "should not swap between builds and sources if asked" do
112
120
  @config.swap_builds_and_sources = false
113
121
  @config.swap_builds_and_sources.should == false
114
122
  end
115
-
123
+
116
124
  it "should not include files automatically if asked" do
117
125
  @config.include_scripts_automatically = false
118
126
  @config.include_scripts_automatically.should == false
119
127
  end
128
+
129
+ it "should use CDN Server in production if asked" do
130
+ @config.use_cdn_in_production = true
131
+ @config.use_cdn_in_production.should == true
132
+ end
133
+
134
+ it "should allow to change the CDN Server url" do
135
+ @config.cdn_url = 'http://some.other.place'
136
+ @config.cdn_url.should == 'http://some.other.place'
137
+ end
120
138
  end
121
-
139
+
122
140
  describe ".dev_env? method" do
123
141
  it "should return true in the 'development' environment" do
124
142
  @config.env = 'development'
125
143
  @config.dev_env?.should == true
126
144
  end
127
-
145
+
128
146
  it "should return false in the 'production' environment" do
129
147
  @config.env = 'production'
130
148
  @config.dev_env?.should == false
@@ -7,119 +7,198 @@ describe RightRails::Helpers do
7
7
  before :each do
8
8
  @config = RightRails::Config
9
9
  @config.reset!
10
-
10
+
11
11
  @helper = RightRails::Helpers
12
12
  @context = {}
13
13
  end
14
-
14
+
15
15
  def require_js_module(*list)
16
16
  @helper.require_js_module @context, *list
17
17
  end
18
-
18
+
19
19
  def required_js_files
20
20
  @helper.required_js_files @context
21
21
  end
22
-
22
+
23
23
  def modules_registry
24
24
  @helper.send(:modules_registry_for, @context)
25
25
  end
26
-
26
+
27
27
  describe ".prefix" do
28
28
  it "should be empty in normal mode" do
29
29
  @config.safe_mode = false
30
30
  @helper.prefix.should == ''
31
31
  end
32
-
32
+
33
33
  it "should return 'RightJS.' in safe-mode" do
34
34
  @config.safe_mode = true
35
35
  @helper.prefix.should == 'RightJS.'
36
36
  end
37
37
  end
38
-
38
+
39
39
  describe ".html_safe" do
40
40
  it "should always return normal strings" do
41
41
  @helper.html_safe('<b>boo!</b>').should == '<b>boo!</b>'
42
42
  end
43
43
  end
44
-
44
+
45
45
  describe ".require_js_module" do
46
46
  it "should require files one by one" do
47
47
  require_js_module 'one'
48
48
  require_js_module :two
49
-
49
+
50
50
  modules_registry.should == ['one', 'two']
51
51
  end
52
-
52
+
53
53
  it "should skip duplicates" do
54
54
  require_js_module 'one'
55
55
  require_js_module :one
56
-
56
+
57
57
  modules_registry.should == ['one']
58
58
  end
59
-
59
+
60
60
  it "should accept several modules" do
61
61
  require_js_module 'one', 'two', 'two'
62
62
  modules_registry.should == ['one', 'two']
63
63
  end
64
64
  end
65
-
65
+
66
66
  describe ".required_js_files" do
67
67
  before :each do
68
68
  @config.swap_builds_and_sources = false
69
69
  end
70
-
70
+
71
71
  it "should include 'right' module first even when nothing set" do
72
72
  required_js_files.first.should == 'right'
73
73
  end
74
-
74
+
75
75
  it "should include the 'right/rails' module when config says so" do
76
76
  @config.include_rails_module = true
77
77
  required_js_files[1].should == 'right/rails'
78
78
  end
79
-
79
+
80
80
  it "should not include the 'right/rails' module if the config doesn't want it" do
81
81
  @config.include_rails_module = false
82
82
  required_js_files.should_not include('right/rails')
83
83
  end
84
-
84
+
85
85
  it "should add the registered modules" do
86
86
  require_js_module 'one', 'two'
87
87
  required_js_files.should == ['right', 'right/rails', 'right/one', 'right/two']
88
88
  end
89
-
89
+
90
90
  it "should swap to the source-files if config asks that and we are in the development-module" do
91
91
  @config.env = 'development'
92
92
  @config.swap_builds_and_sources = true
93
93
  required_js_files.should == ['right-src', 'right/rails-src']
94
94
  end
95
-
95
+
96
96
  it "should not swap to the source files in non-development environments" do
97
97
  @config.env = 'production'
98
98
  @config.swap_builds_and_sources = true
99
99
  required_js_files.should == ['right', 'right/rails']
100
100
  end
101
-
101
+
102
102
  it "should not include nothing but 'right' if config disabled auto-including" do
103
103
  @config.include_scripts_automatically = false
104
104
  require_js_module 'one', 'two'
105
105
  required_js_files.should == ['right']
106
106
  end
107
-
107
+
108
108
  it "should try to include locales when available" do
109
109
  I18n.should_receive(:locale).and_return('boo')
110
110
  @config.should_receive(:public_path).and_return('/public')
111
111
  @config.should_receive(:locales_path).and_return('/public/javascripts/right/i18n')
112
112
  File.should_receive(:exists?).with('/public/javascripts/right/i18n/boo.js').and_return(true)
113
-
113
+
114
114
  required_js_files.should == ['right', 'right/rails', 'right/i18n/boo']
115
115
  end
116
-
116
+
117
117
  it "should not include locales if there are not files for them" do
118
118
  I18n.should_receive(:locale).and_return('boo')
119
119
  @config.should_receive(:locales_path).and_return('/public/javascripts/right/i18n')
120
120
  File.should_receive(:exists?).with('/public/javascripts/right/i18n/boo.js').and_return(false)
121
-
121
+
122
122
  required_js_files.should == ['right', 'right/rails']
123
123
  end
124
+
125
+ describe "with CDN option" do
126
+ before :each do
127
+ @config.env = 'production'
128
+ @config.use_cdn_in_production = true
129
+ @scripts_path = 'rails-root/public/javascripts'
130
+ end
131
+
132
+ def stub_file(name, content='')
133
+ File.should_receive(:read).with("#{@scripts_path}/#{name}", 100).and_return(content)
134
+ end
135
+
136
+ it "should include RightJS core and plugins with CDN urls" do
137
+ stub_file('right.js')
138
+ stub_file('right/rails.js')
139
+ stub_file('right/one.js')
140
+ stub_file('right/two.js')
141
+
142
+ require_js_module 'one', 'two'
143
+ required_js_files.should == [
144
+ "#{@config::DEFAULT_CDN_URL}/right.js",
145
+ "#{@config::DEFAULT_CDN_URL}/plugins/rails.js",
146
+ "#{@config::DEFAULT_CDN_URL}/plugins/one.js",
147
+ "#{@config::DEFAULT_CDN_URL}/plugins/two.js"
148
+ ]
149
+ end
150
+
151
+ it "should include urls with versions when available" do
152
+ stub_file('right.js', '/** some header v2.22.222')
153
+ stub_file('right/rails.js', '/** some header v3.33.333')
154
+ stub_file('right/one.js', 'no version in here')
155
+
156
+ require_js_module 'one'
157
+ required_js_files.should == [
158
+ "#{@config::DEFAULT_CDN_URL}/right-2.22.222.js",
159
+ "#{@config::DEFAULT_CDN_URL}/plugins/rails-3.33.333.js",
160
+ "#{@config::DEFAULT_CDN_URL}/plugins/one.js"
161
+ ]
162
+ end
163
+
164
+ it "should automatically distinct between plugins and UI modules" do
165
+ stub_file('right.js', 'version 2.22.222 something')
166
+ stub_file('right/rails.js', 'version 3.33.333 something')
167
+ stub_file('right/widget.js', 'v4.44.444 see http://rightjs.org/ui/widget')
168
+
169
+ require_js_module 'widget'
170
+ required_js_files.should == [
171
+ "#{@config::DEFAULT_CDN_URL}/right-2.22.222.js",
172
+ "#{@config::DEFAULT_CDN_URL}/plugins/rails-3.33.333.js",
173
+ "#{@config::DEFAULT_CDN_URL}/ui/widget-4.44.444.js"
174
+ ]
175
+ end
176
+
177
+ it "should correctly refer the i18n modules" do
178
+ I18n.should_receive(:locale).and_return('boo')
179
+ File.should_receive(:exists?).with("#{@scripts_path}/right/i18n/boo.js").and_return(true)
180
+
181
+ stub_file('right.js')
182
+ stub_file('right/rails.js')
183
+ stub_file('right/i18n/boo.js')
184
+
185
+ required_js_files.should == [
186
+ "#{@config::DEFAULT_CDN_URL}/right.js",
187
+ "#{@config::DEFAULT_CDN_URL}/plugins/rails.js",
188
+ "#{@config::DEFAULT_CDN_URL}/i18n/boo.js"
189
+ ]
190
+ end
191
+
192
+ it "should use different cdn-url when asked" do
193
+ stub_file('right.js')
194
+ stub_file('right/rails.js')
195
+
196
+ @config.cdn_url = 'http://some.url/dir'
197
+ required_js_files.should == [
198
+ 'http://some.url/dir/right.js',
199
+ 'http://some.url/dir/plugins/rails.js'
200
+ ]
201
+ end
202
+ end
124
203
  end
125
204
  end
data/spec/spec.opts CHANGED
@@ -1,5 +1 @@
1
- --colour
2
- --diff
3
- --format progress
4
- --loadby mtime
5
- --reverse
1
+ --color
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,4 @@
1
- require 'spec'
1
+ require 'rspec'
2
2
 
3
3
  $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
4
4
 
@@ -12,17 +12,17 @@ module Rails
12
12
  def env
13
13
  'production'
14
14
  end
15
-
15
+
16
16
  def root
17
17
  'rails-root'
18
18
  end
19
-
19
+
20
20
  def public_path
21
21
  "#{root}/public"
22
22
  end
23
23
  end
24
-
24
+
25
25
  module VERSION
26
- MAJOR = 2
26
+ MAJOR = 2 unless defined?(MAJOR)
27
27
  end
28
28
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: right-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 12
10
- version: 1.0.12
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Nikolay Nemshilov
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-24 00:00:00 +03:00
18
+ date: 2011-03-01 00:00:00 +03:00
19
19
  default_executable:
20
20
  dependencies: []
21
21