radiant-banner_rotator-extension 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/.gitmodules +3 -0
  2. data/HELP.textile +87 -0
  3. data/README.textile +68 -0
  4. data/Rakefile +136 -0
  5. data/VERSION +1 -0
  6. data/app/controllers/admin/banners_controller.rb +61 -0
  7. data/app/helpers/admin/banners_helper.rb +62 -0
  8. data/app/models/banner.rb +101 -0
  9. data/app/models/banner_placement.rb +7 -0
  10. data/app/views/admin/banners/_form.html.haml +76 -0
  11. data/app/views/admin/banners/_modify_banner.html.haml +8 -0
  12. data/app/views/admin/banners/deactivate.html.haml +10 -0
  13. data/app/views/admin/banners/edit.html.haml +8 -0
  14. data/app/views/admin/banners/index.html.haml +118 -0
  15. data/app/views/admin/banners/new.html.haml +5 -0
  16. data/app/views/admin/banners/remove.html.haml +12 -0
  17. data/app/views/admin/pages/_banner_info.html.haml +28 -0
  18. data/app/views/admin/pages/_banners_column.html.haml +11 -0
  19. data/app/views/admin/pages/_banners_column_header.html.haml +9 -0
  20. data/banner_rotator_extension.rb +41 -0
  21. data/config/initializers/radiant_config.rb +3 -0
  22. data/config/locales/en.yml +34 -0
  23. data/config/routes.rb +5 -0
  24. data/db/migrate/001_create_banners.rb +31 -0
  25. data/db/migrate/002_change_banners_default.rb +9 -0
  26. data/db/migrate/003_add_description.rb +9 -0
  27. data/lib/banner_rotator/page_extensions.rb +47 -0
  28. data/lib/banner_rotator/tags.rb +67 -0
  29. data/lib/tasks/banner_rotator_extension_tasks.rake +29 -0
  30. data/public/images/admin/deactivate.png +0 -0
  31. data/public/images/admin/pictures.png +0 -0
  32. data/public/images/admin/text_list_numbers.png +0 -0
  33. data/public/javascripts/lowpro.js +320 -0
  34. data/spec/controllers/admin_banners_controller_spec.rb +231 -0
  35. data/spec/datasets/banners_dataset.rb +26 -0
  36. data/spec/helpers/admin/banners_helper_spec.rb +9 -0
  37. data/spec/models/banner_placement_spec.rb +44 -0
  38. data/spec/models/banner_spec.rb +47 -0
  39. data/spec/models/page_extensions_spec.rb +25 -0
  40. data/spec/models/tags_spec.rb +69 -0
  41. data/spec/spec.opts +6 -0
  42. data/spec/spec_helper.rb +36 -0
  43. metadata +124 -0
@@ -0,0 +1,9 @@
1
+ class AddDescription < ActiveRecord::Migration
2
+ def self.up
3
+ add_column :banners, :description, :text
4
+ end
5
+
6
+ def self.down
7
+ remove_column :banners, :description
8
+ end
9
+ end
@@ -0,0 +1,47 @@
1
+ module BannerRotator
2
+ module PageExtensions
3
+ def self.included(base)
4
+ base.class_eval do
5
+ has_many :banner_placements
6
+ has_many :banners, :through => :banner_placements
7
+ alias_method_chain :banner_placements, :inheritance
8
+ after_destroy :remove_assigned_banner_placements
9
+ end
10
+ end
11
+
12
+ def banners_assigned?
13
+ show_banner? && ! banners.empty?
14
+ end
15
+
16
+ def banners_inherited?
17
+ show_banner? && banners.empty? && banner_placements.count > 0
18
+ end
19
+
20
+ def banner_placements_with_inheritance
21
+ if banner_placements_without_inheritance.empty? && parent
22
+ parent.banner_placements
23
+ else
24
+ banner_placements_without_inheritance
25
+ end
26
+ end
27
+
28
+ def select_banner
29
+ @banners_with_weighting ||= banner_placements.map do |p|
30
+ [p.banner] * p.weight
31
+ end.flatten
32
+
33
+ x = rand(@banners_with_weighting.size)
34
+ banner = @banners_with_weighting[x]
35
+ @banners_with_weighting.delete(x+1)
36
+
37
+ banner
38
+ end
39
+
40
+ private
41
+
42
+ def remove_assigned_banner_placements
43
+ BannerPlacement.delete_all("page_id = #{id}") if banners_assigned?
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,67 @@
1
+ module BannerRotator::Tags
2
+ include Radiant::Taggable
3
+
4
+ desc %{
5
+ Selects a banner from the rotating banners available to this page.
6
+ If no banner is found for this page and banners are enabled, the page
7
+ will inherit from its parent. If no banners are found, or they are disabled for this page,
8
+ then the tag will not be expanded.
9
+
10
+ *Usage*:
11
+
12
+ <pre><code><r:banner><!-- put some banner output code here --></r:banner></code></pre>
13
+ }
14
+ tag 'banner' do |tag|
15
+ page = tag.locals.page
16
+ tag.locals.banner = page.select_banner
17
+ tag.expand if tag.locals.banner && page.show_banner?
18
+ end
19
+
20
+ %w{name background_image foreground_image link_url link_target image_style description}.each do |att|
21
+ desc %{
22
+ Outputs the #{att} attribute of the current banner.
23
+
24
+ *Usage*:
25
+
26
+ <pre><code><r:banner><r:#{att} /></r:banner></code></pre>
27
+ }
28
+ tag "banner:#{att}" do |tag|
29
+ tag.locals.banner.send(att)
30
+ end
31
+ desc %{
32
+ Expands the contents if there is a non-empty #{att}.
33
+
34
+ *Usage*:
35
+
36
+ <pre><code>
37
+ <r:banner>
38
+ <r:if_#{att}>Content to display</r:if_#{att}>
39
+ </r:banner>
40
+ </code></pre>
41
+ }
42
+ tag "banner:if_#{att}" do |tag|
43
+ tag.expand unless tag.locals.banner[att].blank?
44
+ end
45
+ tag "banner:content" do |tag|
46
+ if tag.locals.banner['background_image'] =~ /swf/i
47
+ "<a href=\"#{tag.locals.banner['link_url']}\" target=\"#{tag.locals.banner['link_target']}\"><object class=\"banner\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"160\" height=\"140\" bgcolor=\"#f0f7fd\"><param name=\"movie\" value=\"#{tag.locals.banner['background_image']}\"><param name=\"quality\" value=\"high\"><embed src=\"#{tag.locals.banner['background_image']}\" quality=\"high\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"160\" height=\"140\" bgcolor=\"#f0f7fd\"></embed></object></a>"
48
+ else
49
+ "<a href=\"#{tag.locals.banner['link_url']}\" target=\"#{tag.locals.banner['link_target']}\"><img src=\"#{tag.locals.banner['background_image']}\" /></a>"
50
+ end
51
+ end
52
+ desc %{
53
+ Expands the contents if there is no #{att}.
54
+
55
+ *Usage*:
56
+
57
+ <pre><code>
58
+ <r:banner>
59
+ <r:unless_#{att}>Content to display</r:unless_#{att}>
60
+ </r:banner>
61
+ </code></pre>
62
+ }
63
+ tag "banner:unless_#{att}" do |tag|
64
+ tag.expand if tag.locals.banner[att].blank?
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,29 @@
1
+ namespace :radiant do
2
+ namespace :extensions do
3
+ namespace :banner_rotator do
4
+
5
+ desc "Runs the migration of the Banner Rotator extension"
6
+ task :migrate => :environment do
7
+ require 'radiant/extension_migrator'
8
+ if ENV["VERSION"]
9
+ BannerRotatorExtension.migrator.migrate(ENV["VERSION"].to_i)
10
+ else
11
+ BannerRotatorExtension.migrator.migrate
12
+ end
13
+ end
14
+
15
+ desc "Copies public assets of the Banner Rotator to the instance public/ directory."
16
+ task :update => :environment do
17
+ is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
18
+ Dir[BannerRotatorExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
19
+ path = file.sub(BannerRotatorExtension.root, '')
20
+ directory = File.dirname(path)
21
+ puts "Copying #{path}..."
22
+ mkdir_p RAILS_ROOT + directory
23
+ cp file, RAILS_ROOT + path
24
+ end
25
+ end
26
+
27
+ end
28
+ end
29
+ end
Binary file
Binary file
@@ -0,0 +1,320 @@
1
+ LowPro = {};
2
+ LowPro.Version = '0.5';
3
+ LowPro.CompatibleWithPrototype = '1.6';
4
+
5
+ if (Prototype.Version.indexOf(LowPro.CompatibleWithPrototype) != 0 && console && console.warn)
6
+ console.warn("This version of Low Pro is tested with Prototype " + LowPro.CompatibleWithPrototype +
7
+ " it may not work as expected with this version (" + Prototype.Version + ")");
8
+
9
+ if (!Element.addMethods)
10
+ Element.addMethods = function(o) { Object.extend(Element.Methods, o) };
11
+
12
+ // Simple utility methods for working with the DOM
13
+ DOM = {};
14
+
15
+ // DOMBuilder for prototype
16
+ DOM.Builder = {
17
+ tagFunc : function(tag) {
18
+ return function() {
19
+ var attrs, children;
20
+ if (arguments.length>0) {
21
+ if (arguments[0].nodeName ||
22
+ typeof arguments[0] == "string")
23
+ children = arguments;
24
+ else {
25
+ attrs = arguments[0];
26
+ children = Array.prototype.slice.call(arguments, 1);
27
+ };
28
+ }
29
+ return DOM.Builder.create(tag, attrs, children);
30
+ };
31
+ },
32
+ create : function(tag, attrs, children) {
33
+ attrs = attrs || {}; children = children || []; tag = tag.toLowerCase();
34
+ var el = new Element(tag, attrs);
35
+
36
+ for (var i=0; i<children.length; i++) {
37
+ if (typeof children[i] == 'string')
38
+ children[i] = document.createTextNode(children[i]);
39
+ el.appendChild(children[i]);
40
+ }
41
+ return $(el);
42
+ }
43
+ };
44
+
45
+ // Automatically create node builders as $tagName.
46
+ (function() {
47
+ var els = ("p|div|span|strong|em|img|table|tr|td|th|thead|tbody|tfoot|pre|code|" +
48
+ "h1|h2|h3|h4|h5|h6|ul|ol|li|form|input|textarea|legend|fieldset|" +
49
+ "select|option|blockquote|cite|br|hr|dd|dl|dt|address|a|button|abbr|acronym|" +
50
+ "script|link|style|bdo|ins|del|object|param|col|colgroup|optgroup|caption|" +
51
+ "label|dfn|kbd|samp|var").split("|");
52
+ var el, i=0;
53
+ while (el = els[i++])
54
+ window['$' + el] = DOM.Builder.tagFunc(el);
55
+ })();
56
+
57
+ DOM.Builder.fromHTML = function(html) {
58
+ var root;
59
+ if (!(root = arguments.callee._root))
60
+ root = arguments.callee._root = document.createElement('div');
61
+ root.innerHTML = html;
62
+ return root.childNodes[0];
63
+ };
64
+
65
+
66
+
67
+ // Wraps the 1.6 contentloaded event for backwards compatibility
68
+ //
69
+ // Usage:
70
+ //
71
+ // Event.onReady(callbackFunction);
72
+ Object.extend(Event, {
73
+ onReady : function(f) {
74
+ if (document.body) f();
75
+ else document.observe('dom:loaded', f);
76
+ }
77
+ });
78
+
79
+ // Based on event:Selectors by Justin Palmer
80
+ // http://encytemedia.com/event-selectors/
81
+ //
82
+ // Usage:
83
+ //
84
+ // Event.addBehavior({
85
+ // "selector:event" : function(event) { /* event handler. this refers to the element. */ },
86
+ // "selector" : function() { /* runs function on dom ready. this refers to the element. */ }
87
+ // ...
88
+ // });
89
+ //
90
+ // Multiple calls will add to exisiting rules. Event.addBehavior.reassignAfterAjax and
91
+ // Event.addBehavior.autoTrigger can be adjusted to needs.
92
+ Event.addBehavior = function(rules) {
93
+ var ab = this.addBehavior;
94
+ Object.extend(ab.rules, rules);
95
+
96
+ if (!ab.responderApplied) {
97
+ Ajax.Responders.register({
98
+ onComplete : function() {
99
+ if (Event.addBehavior.reassignAfterAjax)
100
+ setTimeout(function() { ab.reload() }, 10);
101
+ }
102
+ });
103
+ ab.responderApplied = true;
104
+ }
105
+
106
+ if (ab.autoTrigger) {
107
+ this.onReady(ab.load.bind(ab, rules));
108
+ }
109
+
110
+ };
111
+
112
+ Object.extend(Event.addBehavior, {
113
+ rules : {}, cache : [],
114
+ reassignAfterAjax : false,
115
+ autoTrigger : true,
116
+
117
+ load : function(rules) {
118
+ for (var selector in rules) {
119
+ var observer = rules[selector];
120
+ var sels = selector.split(',');
121
+ sels.each(function(sel) {
122
+ var parts = sel.split(/:(?=[a-z]+$)/), css = parts[0], event = parts[1];
123
+ $$(css).each(function(element) {
124
+ if (event) {
125
+ observer = Event.addBehavior._wrapObserver(observer);
126
+ $(element).observe(event, observer);
127
+ Event.addBehavior.cache.push([element, event, observer]);
128
+ } else {
129
+ if (!element.$$assigned || !element.$$assigned.include(observer)) {
130
+ if (observer.attach) observer.attach(element);
131
+
132
+ else observer.call($(element));
133
+ element.$$assigned = element.$$assigned || [];
134
+ element.$$assigned.push(observer);
135
+ }
136
+ }
137
+ });
138
+ });
139
+ }
140
+ },
141
+
142
+ unload : function() {
143
+ this.cache.each(function(c) {
144
+ Event.stopObserving.apply(Event, c);
145
+ });
146
+ this.cache = [];
147
+ },
148
+
149
+ reload: function() {
150
+ var ab = Event.addBehavior;
151
+ ab.unload();
152
+ ab.load(ab.rules);
153
+ },
154
+
155
+ _wrapObserver: function(observer) {
156
+ return function(event) {
157
+ if (observer.call(this, event) === false) event.stop();
158
+ }
159
+ }
160
+
161
+ });
162
+
163
+ Event.observe(window, 'unload', Event.addBehavior.unload.bind(Event.addBehavior));
164
+
165
+ // A silly Prototype style shortcut for the reckless
166
+ $$$ = Event.addBehavior.bind(Event);
167
+
168
+ // Behaviors can be bound to elements to provide an object orientated way of controlling elements
169
+ // and their behavior. Use Behavior.create() to make a new behavior class then use attach() to
170
+ // glue it to an element. Each element then gets it's own instance of the behavior and any
171
+ // methods called onxxx are bound to the relevent event.
172
+ //
173
+ // Usage:
174
+ //
175
+ // var MyBehavior = Behavior.create({
176
+ // onmouseover : function() { this.element.addClassName('bong') }
177
+ // });
178
+ //
179
+ // Event.addBehavior({ 'a.rollover' : MyBehavior });
180
+ //
181
+ // If you need to pass additional values to initialize use:
182
+ //
183
+ // Event.addBehavior({ 'a.rollover' : MyBehavior(10, { thing : 15 }) })
184
+ //
185
+ // You can also use the attach() method. If you specify extra arguments to attach they get passed to initialize.
186
+ //
187
+ // MyBehavior.attach(el, values, to, init);
188
+ //
189
+ // Finally, the rawest method is using the new constructor normally:
190
+ // var draggable = new Draggable(element, init, vals);
191
+ //
192
+ // Each behaviour has a collection of all its instances in Behavior.instances
193
+ //
194
+ var Behavior = {
195
+ create: function() {
196
+ var parent = null, properties = $A(arguments);
197
+ if (Object.isFunction(properties[0]))
198
+ parent = properties.shift();
199
+
200
+ var behavior = function() {
201
+ var behavior = arguments.callee;
202
+ if (!this.initialize) {
203
+ var args = $A(arguments);
204
+
205
+ return function() {
206
+ var initArgs = [this].concat(args);
207
+ behavior.attach.apply(behavior, initArgs);
208
+ };
209
+ } else {
210
+ var args = (arguments.length == 2 && arguments[1] instanceof Array) ?
211
+ arguments[1] : Array.prototype.slice.call(arguments, 1);
212
+
213
+ this.element = $(arguments[0]);
214
+ this.initialize.apply(this, args);
215
+ behavior._bindEvents(this);
216
+ behavior.instances.push(this);
217
+ }
218
+ };
219
+
220
+ Object.extend(behavior, Class.Methods);
221
+ Object.extend(behavior, Behavior.Methods);
222
+ behavior.superclass = parent;
223
+ behavior.subclasses = [];
224
+ behavior.instances = [];
225
+
226
+ if (parent) {
227
+ var subclass = function() { };
228
+ subclass.prototype = parent.prototype;
229
+ behavior.prototype = new subclass;
230
+ parent.subclasses.push(behavior);
231
+ }
232
+
233
+ for (var i = 0; i < properties.length; i++)
234
+ behavior.addMethods(properties[i]);
235
+
236
+ if (!behavior.prototype.initialize)
237
+ behavior.prototype.initialize = Prototype.emptyFunction;
238
+
239
+ behavior.prototype.constructor = behavior;
240
+
241
+ return behavior;
242
+ },
243
+ Methods : {
244
+ attach : function(element) {
245
+ return new this(element, Array.prototype.slice.call(arguments, 1));
246
+ },
247
+ _bindEvents : function(bound) {
248
+ for (var member in bound)
249
+ if (member.match(/^on(.+)/) && typeof bound[member] == 'function')
250
+ bound.element.observe(RegExp.$1, Event.addBehavior._wrapObserver(bound[member].bindAsEventListener(bound)));
251
+ }
252
+ }
253
+ };
254
+
255
+ Remote = Behavior.create({
256
+ initialize: function(options) {
257
+ if (this.element.nodeName == 'FORM') new Remote.Form(this.element, options);
258
+ else new Remote.Link(this.element, options);
259
+ }
260
+ });
261
+
262
+ Remote.Base = {
263
+ initialize : function(options) {
264
+ this.options = Object.extend({
265
+ evaluateScripts : true
266
+ }, options || {});
267
+ },
268
+ _makeRequest : function(options) {
269
+ if (options.update) new Ajax.Updater(options.update, options.url, options);
270
+ else new Ajax.Request(options.url, options);
271
+ return false;
272
+ }
273
+ }
274
+
275
+ Remote.Link = Behavior.create(Remote.Base, {
276
+ onclick : function() {
277
+ var options = Object.extend({ url : this.element.href, method : 'get' }, this.options);
278
+ return this._makeRequest(options);
279
+ }
280
+ });
281
+
282
+
283
+ Remote.Form = Behavior.create(Remote.Base, {
284
+ onclick : function(e) {
285
+ var sourceElement = e.element();
286
+
287
+ if (['input', 'button'].include(sourceElement.nodeName.toLowerCase()) &&
288
+ sourceElement.type == 'submit')
289
+ this._submitButton = sourceElement;
290
+ },
291
+ onsubmit : function() {
292
+ var options = Object.extend({
293
+ url : this.element.action,
294
+ method : this.element.method || 'get',
295
+ parameters : this.element.serialize({ submit: this._submitButton.name })
296
+ }, this.options);
297
+ this._submitButton = null;
298
+ return this._makeRequest(options);
299
+ }
300
+ });
301
+
302
+ Observed = Behavior.create({
303
+ initialize : function(callback, options) {
304
+ this.callback = callback.bind(this);
305
+ this.options = options || {};
306
+ this.observer = (this.element.nodeName == 'FORM') ? this._observeForm() : this._observeField();
307
+ },
308
+ stop: function() {
309
+ this.observer.stop();
310
+ },
311
+ _observeForm: function() {
312
+ return (this.options.frequency) ? new Form.Observer(this.element, this.options.frequency, this.callback) :
313
+ new Form.EventObserver(this.element, this.callback);
314
+ },
315
+ _observeField: function() {
316
+ return (this.options.frequency) ? new Form.Element.Observer(this.element, this.options.frequency, this.callback) :
317
+ new Form.Element.EventObserver(this.element, this.callback);
318
+ }
319
+ });
320
+