puffer 0.0.7 → 0.0.8

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.
Files changed (48) hide show
  1. data/VERSION +1 -1
  2. data/app/cells/puffer/base/additional.html.erb +13 -5
  3. data/app/helpers/puffer_helper.rb +6 -1
  4. data/app/views/layouts/puffer.html.erb +5 -3
  5. data/app/views/puffer/_form.html.erb +1 -1
  6. data/app/views/puffer/associated/one.js.erb +9 -0
  7. data/app/views/puffer/edit.html.erb +1 -1
  8. data/app/views/puffer/index.html.erb +7 -7
  9. data/app/views/puffer/new.html.erb +1 -1
  10. data/app/views/puffer/show.html.erb +4 -4
  11. data/lib/generators/puffer/install/templates/puffer/javascripts/application.js +10 -0
  12. data/lib/generators/puffer/install/templates/puffer/javascripts/rails-src.js +36 -378
  13. data/lib/generators/puffer/install/templates/puffer/javascripts/right-autocompleter-src.js +621 -0
  14. data/lib/generators/puffer/install/templates/puffer/javascripts/right-autocompleter.js +18 -0
  15. data/lib/generators/puffer/install/templates/puffer/javascripts/right-autocompleter.js.gz +0 -0
  16. data/lib/generators/puffer/install/templates/puffer/stylesheets/puffer.css +107 -6
  17. data/lib/puffer/base.rb +4 -3
  18. data/lib/puffer/controller/actions.rb +4 -70
  19. data/lib/puffer/controller/dsl.rb +23 -5
  20. data/lib/puffer/controller/generated.rb +65 -0
  21. data/lib/puffer/extensions/activerecord.rb +3 -3
  22. data/lib/puffer/extensions/core.rb +14 -0
  23. data/lib/puffer/extensions/mapper.rb +20 -0
  24. data/lib/puffer/fields.rb +11 -2
  25. data/lib/puffer/fields/field.rb +24 -1
  26. data/lib/puffer/inputs.rb +1 -3
  27. data/lib/puffer/inputs/association.rb +27 -1
  28. data/lib/puffer/inputs/base.rb +12 -4
  29. data/lib/puffer/inputs/boolean.rb +1 -1
  30. data/lib/puffer/inputs/date_time.rb +1 -1
  31. data/lib/puffer/resource.rb +1 -1
  32. data/lib/puffer/resource/routing.rb +2 -2
  33. data/lib/puffer/resource/scoping.rb +5 -1
  34. data/puffer.gemspec +15 -5
  35. data/spec/dummy/app/controllers/admin/profiles_controller.rb +2 -2
  36. data/spec/dummy/app/views/admin/users/index.html.erb +3 -1
  37. data/spec/dummy/config/database.yml +1 -1
  38. data/spec/dummy/public/puffer/javascripts/application.js +10 -0
  39. data/spec/dummy/public/puffer/javascripts/rails-src.js +36 -378
  40. data/spec/dummy/public/puffer/javascripts/right-autocompleter-src.js +621 -0
  41. data/spec/dummy/public/puffer/javascripts/right-autocompleter.js +18 -0
  42. data/spec/dummy/public/puffer/javascripts/right-autocompleter.js.gz +0 -0
  43. data/spec/dummy/public/puffer/stylesheets/puffer.css +107 -6
  44. data/spec/lib/extensions/core_spec.rb +29 -0
  45. data/spec/lib/resource/routing_spec.rb +11 -11
  46. metadata +28 -18
  47. data/lib/generators/puffer/install/templates/puffer/javascripts/rails.js +0 -14
  48. data/spec/dummy/public/puffer/javascripts/rails.js +0 -14
@@ -60,6 +60,11 @@ module Puffer
60
60
 
61
61
  collection do
62
62
  post :create, options
63
+ controller._collections.each do |args|
64
+ opts = args.extract_options!.dup
65
+ args.push options.reverse_merge(opts)
66
+ send *args
67
+ end
63
68
  end
64
69
 
65
70
  new do
@@ -71,6 +76,11 @@ module Puffer
71
76
  get :show, options
72
77
  put :update, options
73
78
  delete :destroy, options
79
+ controller._members.each do |args|
80
+ opts = args.extract_options!.dup
81
+ args.push options.reverse_merge(opts)
82
+ send *args
83
+ end
74
84
  end
75
85
 
76
86
  end
@@ -111,6 +121,11 @@ module Puffer
111
121
  collection do
112
122
  get :index, options
113
123
  post :create, options
124
+ controller._collections.each do |args|
125
+ opts = args.extract_options!.dup
126
+ args.push options.reverse_merge(opts)
127
+ send *args
128
+ end
114
129
  end
115
130
 
116
131
  new do
@@ -122,6 +137,11 @@ module Puffer
122
137
  get :show, options
123
138
  put :update, options
124
139
  delete :destroy, options
140
+ controller._members.each do |args|
141
+ opts = args.extract_options!.dup
142
+ args.push options.reverse_merge(opts)
143
+ send *args
144
+ end
125
145
  end
126
146
  end
127
147
 
data/lib/puffer/fields.rb CHANGED
@@ -19,20 +19,29 @@ module Puffer
19
19
  :password if field.name =~ /password/
20
20
  end
21
21
  offer_type do |field|
22
- field.model.reflect_on_association(field.name.to_sym).macro if field.model.reflect_on_association field.name.to_sym
22
+ swallow_nil{ field.reflection.macro }
23
23
  end
24
24
 
25
25
  def field *args
26
26
  push Field.new(*args)
27
+ last
27
28
  end
28
29
 
29
30
  def searchable
30
- @searchable ||= reject { |f| ![:text, :string, :integer, :decimal, :float].include? f.type }
31
+ @searchable ||= map { |f| f if f.column && [:text, :string, :integer, :decimal, :float].include?(f.column.type) }.compact
32
+ end
33
+
34
+ def searches query
35
+ searchable.map { |f| "#{f.query_column} like '%#{query}%'" if f.query_column.present? }.compact.join(' or ') if query
31
36
  end
32
37
 
33
38
  def boolean
34
39
  @boolean ||= reject { |f| f.type != :boolean }
35
40
  end
36
41
 
42
+ def includes
43
+ @includes ||= map {|f| f.path unless f.native?}.compact.to_includes
44
+ end
45
+
37
46
  end
38
47
  end
@@ -15,7 +15,11 @@ module Puffer
15
15
  end
16
16
 
17
17
  def name
18
- field.split('.').last
18
+ @name ||= field.split('.').last
19
+ end
20
+
21
+ def path
22
+ @path ||= field.split('.')[0..-2].join('.')
19
23
  end
20
24
 
21
25
  def label
@@ -34,6 +38,14 @@ module Puffer
34
38
  field
35
39
  end
36
40
 
41
+ def reflection
42
+ @reflection ||= model.reflect_on_association(name.to_sym)
43
+ end
44
+
45
+ def collection?
46
+ [:has_many, :has_and_belongs_to_many].include? type
47
+ end
48
+
37
49
  def input_options
38
50
  {}
39
51
  end
@@ -50,6 +62,17 @@ module Puffer
50
62
  @model
51
63
  end
52
64
 
65
+ def association_fields
66
+ raise "Can`t find records for association building. Please set :search_fields option for '#{field}' field." unless options[:search_fields].present?
67
+ @reflection_fields ||= begin
68
+ fields = Puffer::Fields.new
69
+ options[:search_fields].each do |field_name|
70
+ fields.field reflection.klass, field_name
71
+ end
72
+ fields
73
+ end
74
+ end
75
+
53
76
  def column
54
77
  @column ||= model.columns_hash[name]
55
78
  end
data/lib/puffer/inputs.rb CHANGED
@@ -6,7 +6,6 @@ module Puffer
6
6
 
7
7
  def self.map_type *args
8
8
  to = args.extract_options![:to]
9
- raise ArgumentError, "You need to give :to as option to map_type" unless to
10
9
  args.each { |type| mappings[type] = to }
11
10
  end
12
11
 
@@ -15,8 +14,7 @@ module Puffer
15
14
  map_type :date, :time, :datetime, :timestamp, :to => Puffer::Inputs::DateTime
16
15
 
17
16
  def self.map_field field
18
- mappings[field.type] || (const_defined?(field.type.to_s.classify) ?
19
- "Puffer::Inputs::#{field.type.to_s.classify}".constantize : Puffer::Inputs::Base)
17
+ mappings[field.type] || ("Puffer::Inputs::#{field.type.to_s.classify}".constantize rescue Puffer::Inputs::Base)
20
18
  end
21
19
 
22
20
  end
@@ -3,7 +3,33 @@ module Puffer
3
3
  class Association < Puffer::Inputs::Base
4
4
 
5
5
  def input
6
- field.type
6
+ <<-INPUT
7
+ <div class="association">
8
+ #{text_field_tag field, value, field.input_options.merge(
9
+ :autocomplete => :off,
10
+ :disabled => (true if builder.object.send(field.name)),
11
+ 'data-autocompleter' => "{url: '#{resource.collection_path(:action => "associated_#{field}_choosing")}', onDone: association_done}"
12
+ )}
13
+ <div class="association_clear">×</div>
14
+ #{builder.hidden_field field.reflection.primary_key_name}
15
+ </div>
16
+ INPUT
17
+ end
18
+
19
+ def value
20
+ value = [
21
+ swallow_nil{builder.object.send(field.name)[field.reflection.primary_key_column.name.to_sym]},
22
+ swallow_nil{builder.object.send(field.name).to_title}
23
+ ].compact.join(' - ')
24
+ end
25
+
26
+ def label
27
+ label_tag field
28
+ end
29
+
30
+ def error
31
+ builder.object.errors[field.reflection.primary_key_name.to_sym].first ||
32
+ builder.object.errors[field.name.to_sym].first.presence
7
33
  end
8
34
 
9
35
  end
@@ -11,13 +11,17 @@ module Puffer
11
11
  end
12
12
 
13
13
  def render
14
- template.html_safe
14
+ html.html_safe
15
15
  end
16
16
 
17
- def template
17
+ def html
18
18
  <<-INPUT
19
- #{label}
20
- #{error}
19
+ <div class="label">
20
+ #{label}
21
+ <div class="field_error">
22
+ #{error}
23
+ </div>
24
+ </div>
21
25
  #{input}
22
26
  INPUT
23
27
  end
@@ -34,6 +38,10 @@ module Puffer
34
38
  builder.object.errors[field.name.to_sym].first
35
39
  end
36
40
 
41
+ def method_missing method, *args, &block
42
+ template.send method, *args, &block if template.respond_to? method
43
+ end
44
+
37
45
  end
38
46
  end
39
47
  end
@@ -2,7 +2,7 @@ module Puffer
2
2
  module Inputs
3
3
  class Boolean < Puffer::Inputs::Base
4
4
 
5
- def template
5
+ def html
6
6
  <<-INPUT
7
7
  #{input}
8
8
  #{label}
@@ -8,7 +8,7 @@ module Puffer
8
8
  when :time then '%H:%M:%S'
9
9
  when :datetime, :timestamp then '%Y-%m-%d %H:%M:%S'
10
10
  end
11
- builder.text_field field, field.input_options.merge("data-calendar" => "{showButtons: true, listYears: true, format: '#{format}'}")
11
+ builder.text_field field, field.input_options.merge("data-calendar" => "{showButtons: true, listYears: true, format: '#{format}'}", :autocomplete => :off)
12
12
  end
13
13
 
14
14
  end
@@ -96,7 +96,7 @@ module Puffer
96
96
 
97
97
  def collection
98
98
  scope = parent ? parent.member.send(model_name.pluralize) : model
99
- scope.includes(includes).joins(includes).order(order).paginate :page => params[:page]
99
+ scope.includes(includes).joins(includes).where(searches(params[:search])).order(order).paginate :page => params[:page]
100
100
  end
101
101
 
102
102
  def member
@@ -5,11 +5,11 @@ module Puffer
5
5
  include ActionController::UrlFor
6
6
  include Rails.application.routes.url_helpers
7
7
 
8
- def index_url *args
8
+ def collection_url *args
9
9
  polymorphic_url *route_args(route_member(controller_name), *args)
10
10
  end
11
11
 
12
- def url *args
12
+ def member_url *args
13
13
  suggest = args.shift if args.first.is_a? ActiveRecord::Base
14
14
  polymorphic_url *route_args(route_member(suggest), *args)
15
15
  end
@@ -3,13 +3,17 @@ module Puffer
3
3
  module Scoping
4
4
 
5
5
  def includes
6
-
6
+ controller.index_fields.includes
7
7
  end
8
8
 
9
9
  def order
10
10
 
11
11
  end
12
12
 
13
+ def searches query
14
+ controller.index_fields.searches query
15
+ end
16
+
13
17
  end
14
18
  end
15
19
  end
data/puffer.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{puffer}
8
- s.version = "0.0.7"
8
+ s.version = "0.0.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["pyromaniac"]
12
- s.date = %q{2011-01-28}
12
+ s.date = %q{2011-02-02}
13
13
  s.description = %q{In Soviet Russia puffer admins you}
14
14
  s.email = %q{kinwizard@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -34,6 +34,7 @@ Gem::Specification.new do |s|
34
34
  "app/views/puffer/associated/_many.html.erb",
35
35
  "app/views/puffer/associated/_one.html.erb",
36
36
  "app/views/puffer/associated/many.rjs",
37
+ "app/views/puffer/associated/one.js.erb",
37
38
  "app/views/puffer/associated/one.rjs",
38
39
  "app/views/puffer/association/_many.html.erb",
39
40
  "app/views/puffer/association/_one.html.erb",
@@ -50,8 +51,11 @@ Gem::Specification.new do |s|
50
51
  "lib/generators/puffer/install/USAGE",
51
52
  "lib/generators/puffer/install/install_generator.rb",
52
53
  "lib/generators/puffer/install/templates/puffer.rb",
54
+ "lib/generators/puffer/install/templates/puffer/javascripts/application.js",
53
55
  "lib/generators/puffer/install/templates/puffer/javascripts/rails-src.js",
54
- "lib/generators/puffer/install/templates/puffer/javascripts/rails.js",
56
+ "lib/generators/puffer/install/templates/puffer/javascripts/right-autocompleter-src.js",
57
+ "lib/generators/puffer/install/templates/puffer/javascripts/right-autocompleter.js",
58
+ "lib/generators/puffer/install/templates/puffer/javascripts/right-autocompleter.js.gz",
55
59
  "lib/generators/puffer/install/templates/puffer/javascripts/right-calendar-src.js",
56
60
  "lib/generators/puffer/install/templates/puffer/javascripts/right-calendar.js",
57
61
  "lib/generators/puffer/install/templates/puffer/javascripts/right-calendar.js.gz",
@@ -77,6 +81,7 @@ Gem::Specification.new do |s|
77
81
  "lib/puffer/controller/actions.rb",
78
82
  "lib/puffer/controller/config.rb",
79
83
  "lib/puffer/controller/dsl.rb",
84
+ "lib/puffer/controller/generated.rb",
80
85
  "lib/puffer/controller/helpers.rb",
81
86
  "lib/puffer/controller/mutate.rb",
82
87
  "lib/puffer/engine.rb",
@@ -154,8 +159,11 @@ Gem::Specification.new do |s|
154
159
  "spec/dummy/public/javascripts/effects.js",
155
160
  "spec/dummy/public/javascripts/prototype.js",
156
161
  "spec/dummy/public/javascripts/rails.js",
162
+ "spec/dummy/public/puffer/javascripts/application.js",
157
163
  "spec/dummy/public/puffer/javascripts/rails-src.js",
158
- "spec/dummy/public/puffer/javascripts/rails.js",
164
+ "spec/dummy/public/puffer/javascripts/right-autocompleter-src.js",
165
+ "spec/dummy/public/puffer/javascripts/right-autocompleter.js",
166
+ "spec/dummy/public/puffer/javascripts/right-autocompleter.js.gz",
159
167
  "spec/dummy/public/puffer/javascripts/right-calendar-src.js",
160
168
  "spec/dummy/public/puffer/javascripts/right-calendar.js",
161
169
  "spec/dummy/public/puffer/javascripts/right-calendar.js.gz",
@@ -184,6 +192,7 @@ Gem::Specification.new do |s|
184
192
  "spec/fabricators/tags_fabricator.rb",
185
193
  "spec/fabricators/users_fabricator.rb",
186
194
  "spec/integration/navigation_spec.rb",
195
+ "spec/lib/extensions/core_spec.rb",
187
196
  "spec/lib/fields_spec.rb",
188
197
  "spec/lib/params_spec.rb",
189
198
  "spec/lib/render_fallback_spec.rb",
@@ -194,7 +203,7 @@ Gem::Specification.new do |s|
194
203
  ]
195
204
  s.homepage = %q{http://github.com/puffer/puffer}
196
205
  s.require_paths = ["lib"]
197
- s.rubygems_version = %q{1.4.1}
206
+ s.rubygems_version = %q{1.4.2}
198
207
  s.summary = %q{Admin interface builder}
199
208
  s.test_files = [
200
209
  "spec/dummy/app/controllers/admin/categories_controller.rb",
@@ -240,6 +249,7 @@ Gem::Specification.new do |s|
240
249
  "spec/fabricators/tags_fabricator.rb",
241
250
  "spec/fabricators/users_fabricator.rb",
242
251
  "spec/integration/navigation_spec.rb",
252
+ "spec/lib/extensions/core_spec.rb",
243
253
  "spec/lib/fields_spec.rb",
244
254
  "spec/lib/params_spec.rb",
245
255
  "spec/lib/render_fallback_spec.rb",
@@ -5,14 +5,14 @@ class Admin::ProfilesController < Puffer::Base
5
5
  end
6
6
 
7
7
  index do
8
- field :user
8
+ field 'user.email'
9
9
  field :name
10
10
  field :surname
11
11
  field :birth_date
12
12
  end
13
13
 
14
14
  form do
15
- field :user
15
+ field :user, :search_fields => [:email, :password]
16
16
  field :name
17
17
  field :surname
18
18
  field :birth_date
@@ -1 +1,3 @@
1
- HELO!!!
1
+ <%= render :template => 'puffer/index' %>
2
+
3
+ Hello from admin/users/index.
@@ -17,6 +17,6 @@ test:
17
17
 
18
18
  production:
19
19
  adapter: sqlite3
20
- database: db/production.sqlite3
20
+ database: db/development.sqlite3
21
21
  pool: 5
22
22
  timeout: 5000
@@ -0,0 +1,10 @@
1
+ var association_done = function(event) {
2
+ current = this.first('li.current')
3
+ this.input.next('input[type=hidden]').value(current.get('data-id'));
4
+ this.input.value(current.find('.title').first().html().stripTags()).disable();
5
+ }
6
+
7
+ '.association_clear'.on('click', function(event) {
8
+ this.prev('input[type=text]').value('').enable();
9
+ this.next('input[type=hidden]').value('');
10
+ });
@@ -1,115 +1,16 @@
1
- /**
2
- * The Ruby On Rails plugin for RightJS
3
- * http://github.com/MadRabbit/right-rails
4
- *
5
- * Copyright (C) 2009-2010 Nikolay Nemshilov
6
- */
7
1
  (function(window, document, RightJS) {
8
- /**
9
- * Underscored aliases for Ruby On Rails
10
- *
11
- * Copyright (C) 2009-2010 Nikolay Nemshilov
12
- */
13
2
 
14
- /**
15
- * The Rails plugin initialization script
16
- *
17
- * Copyright (C) 2010 Nikolay Nemshilov
18
- */
3
+ var $ = RightJS.$,
4
+ $$ = RightJS.$$,
5
+ $E = RightJS.$E,
6
+ Xhr = RightJS.Xhr;
19
7
 
20
- var R = RightJS,
21
- $ = RightJS.$,
22
- $$ = RightJS.$$,
23
- $E = RightJS.$E,
24
- Xhr = RightJS.Xhr,
25
- Object = RightJS.Object;
26
-
27
-
28
-
29
- // the language and window level aliases
30
- R([
31
- RightJS.String.prototype,
32
- RightJS.Array.prototype,
33
- RightJS.Function.prototype,
34
- RightJS.Object,
35
- RightJS.Options,
36
- RightJS.Observer,
37
- RightJS.Observer.prototype,
38
- window,
39
- document
40
- ]).each(function(object) {
41
- for (var key in object) {
42
- try { // some keys are not accessable
43
-
44
- if (/[A-Z]/.test(key) && typeof(object[key]) === 'function') {
45
- var u_key = R(key).underscored();
46
- if (object[u_key] === null || object[u_key] === undefined) {
47
- object[u_key] = object[key];
48
- }
49
- }
50
- } catch (e) {}
51
- }
52
- });
53
-
54
-
55
- // DOM package aliases
56
- R([
57
- RightJS.Element,
58
- RightJS.Event,
59
- RightJS.Form,
60
- RightJS.Input
61
- ]).each(function(object) {
62
- if (!object) { return; }
63
-
64
- var aliases = {}, methods = object.prototype;
65
-
66
- for (var key in methods) {
67
- if (/[A-Z]/.test(key) && typeof(methods[key]) === 'function') {
68
- object.prototype[R(key).underscored()] = methods[key];
69
- }
8
+ var cancel = function(element) {
9
+ var message = element.get('data-confirm');
10
+ return(message && !confirm(message));
70
11
  }
71
- });
72
12
 
73
- // various ruby-like method aliases
74
- RightJS.$alias(RightJS.String.prototype, {
75
- index_of: 'indexOf',
76
- last_index_of: 'lastIndexOf',
77
- to_f: 'toFloat',
78
- to_i: 'toInt',
79
- gsub: 'replace',
80
- downcase: 'toLowerCase',
81
- upcase: 'toUpperCase',
82
- index: 'indexOf',
83
- rindex: 'lastIndexOf',
84
- strip: 'trim'
85
- });
86
-
87
- RightJS.$alias(RightJS.Array.prototype, {
88
- collect: 'map',
89
- detect: 'filter',
90
- index_of: 'indexOf',
91
- last_index_of: 'lastIndexOf',
92
- index: 'indexOf',
93
- rindex: 'lastIndexOf'
94
- });
95
-
96
- /**
97
- * Rails 3 UJS support module
98
- *
99
- * Copyright (C) 2010 Nikolay Nemshilov
100
- */
101
- (function() {
102
- // tries to cancel the event via confirmation
103
- var user_cancels = function(event, element) {
104
- var message = element.get('data-confirm');
105
- if (message && !confirm(message)) {
106
- event.stop();
107
- return true;
108
- }
109
- };
110
-
111
- // adds XHR events to the element
112
- var add_xhr_events = function(element, options) {
13
+ var xhr_events = function(element, options) {
113
14
  return Object.merge({
114
15
  onCreate: function() { element.fire('ajax:loading', this); },
115
16
  onComplete: function() { element.fire('ajax:complete', this); },
@@ -117,283 +18,40 @@ RightJS.$alias(RightJS.Array.prototype, {
117
18
  onFailure: function() { element.fire('ajax:failure', this); }
118
19
  }, options);
119
20
  };
120
-
121
- // processes link clicks
122
- var try_link_submit = function(event) {
123
- var link = event.target,
124
- method = link.get('data-method'),
125
- remote = link.get('data-remote'),
126
- url = link.get('href');
127
-
128
- if (user_cancels(event, link)) { return; }
129
- if (method || remote) { event.stop(); }
130
-
21
+
22
+ 'form[data-remote]'.on('submit', function(event) {
23
+ event.stop();
24
+ this.send();
25
+ });
26
+
27
+ 'a[data-confirm], a[data-method], a[data-remote]'.on('click', function(event) {
28
+ if (cancel(this)) { event.stop(); return; }
29
+
30
+ var method = this.get('data-method') || 'get',
31
+ remote = !!this.get('data-remote'),
32
+ url = this.get('href');
33
+
34
+ if (method != 'get' || remote) { event.stop(); }
35
+
131
36
  if (remote) {
132
- Xhr.load(url, add_xhr_events(link, {
133
- method: method || 'get',
134
- spinner: link.get('data-spinner')
37
+ Xhr.load(url, xhr_events(this, {
38
+ method: method,
39
+ spinner: this.get('data-spinner')
135
40
  }));
136
-
137
- } else if (method) {
138
- var param = $$('meta[name=csrf-param]')[0],
139
- token = $$('meta[name=csrf-token]')[0],
41
+ }
42
+
43
+ if (method != 'get') {
44
+ var param = $$('meta[name=csrf-param]')[0].get('content'),
45
+ token = $$('meta[name=csrf-token]')[0].get('content'),
140
46
  form = $E('form', {action: url, method: 'post'});
141
-
47
+
142
48
  if (param && token) {
143
- form.insert('<input type="hidden" name="'+param.get('content')+'" value="'+token.get('content')+'" />');
49
+ form.insert('<input type="hidden" name="' + param + '" value="' + token + '" />');
144
50
  }
145
-
146
- form.insert('<input type="hidden" name="_method" value="'+method+'"/>')
147
- .insertTo(document.body).submit();
148
- }
149
- };
150
51
 
151
- // global events listeners
152
- $(document).on({
153
- click: function(event) {
154
- var tag = event.target._.tagName;
155
- if (tag === 'A' || tag === 'BUTTON') {
156
- try_link_submit(event);
157
- }
158
- },
159
-
160
- submit: function(event) {
161
- var form = event.target;
162
- if (form.has('data-remote') && !user_cancels(event, form)) {
163
- event.stop();
164
- form.send(add_xhr_events(form, {
165
- spinner: form.get('data-spinner') || form.first('.spinner')
166
- }));
167
- }
52
+ form.insert('<input type="hidden" name="_method" value="' + method + '"/>')
53
+ .insertTo(document.body).submit();
168
54
  }
169
55
  });
170
- })();
171
-
172
-
173
- /**
174
- * RR is the common ajax operations wrapper for ruby on rails
175
- *
176
- * Copyright (C) 2009-2010 Nikolay Nemshilov
177
- */
178
- var RR = {
179
- /**
180
- * Basic options
181
- *
182
- * NOTE: DO NOT CHANGE this hash right here
183
- * Use your application.js file to alter the options
184
- */
185
- Options: {
186
- format: 'js', // the working format for remote requests over the application
187
-
188
- flashId: 'flashes', // the flashes element id
189
- flashHideFx: 'slide', // use null if you don't want any fx in here
190
- flashHideDelay: 3200, // use -1 to disable the flash element hidding
191
-
192
- highlightUpdates: true,
193
-
194
- removeFx: 'fade', // blocks removing fx
195
- insertFx: 'fade', // blocks insertion fx
196
-
197
- insertPosition: 'bottom', // default insert position
198
-
199
- linkToAjaxEdit: '.ajax_edit',
200
- linkToAjaxDelete: '.ajax_delete',
201
-
202
- rescanWithScopes: true // if it should rescan only updated elements
203
- },
204
-
205
- /**
206
- * Updates the flashes block with the source
207
- *
208
- * @param String new content
209
- * @return RR this
210
- */
211
- update_flash: function(content) {
212
- var element = $(this.Options.flashId);
213
- if (element) {
214
- this.replace(element, content).hide_flash();
215
- }
216
- return this;
217
- },
218
-
219
- /**
220
- * Initializes the delayed flashes hide call
221
- *
222
- * @return RR this
223
- */
224
- hide_flash: function() {
225
- if (this.Options.flashHideDelay > -1) {
226
- var element = $(this.Options.flashId);
227
- if (element && element.visible()) {
228
- element.hide.bind(element, this.Options.flashHideFx).delay(this.Options.flashHideDelay);
229
- }
230
- }
231
- return this;
232
- },
233
-
234
- /**
235
- * Highlights the element according to the options
236
- *
237
- * @param String element id
238
- * @return RR this
239
- */
240
- highlight: function(id) {
241
- if ($(id) && this.Options.highlightUpdates) {
242
- $(id).highlight();
243
- }
244
- return this;
245
- },
246
-
247
- /**
248
- * Inserts the content into the given element
249
- *
250
- * @param destination String destination id
251
- * @param content String content
252
- * @param position String position
253
- * @return RR this
254
- */
255
- insert: function(where, what, in_position) {
256
- var position = in_position || this.Options.insertPosition, new_element,
257
- container = $(where).insert(what, position);
258
-
259
- // trying to find the new block
260
- switch (position) {
261
- case 'bottom': new_element = container.children().last(); break;
262
- case 'top': new_element = container.first(); break;
263
- case 'before': new_element = container.prev(); break;
264
- case 'after': new_element = container.next(); break;
265
- }
266
-
267
- // necely displaying the new block
268
- if (new_element && this.Options.insertFx) {
269
- new_element.hide().show(this.Options.insertFx, {
270
- onFinish: this.highlight.bind(this, new_element)
271
- });
272
- } else {
273
- this.highlight(new_element);
274
- }
275
-
276
- return this.rescan(where);
277
- },
278
-
279
- /**
280
- * Replaces the given element with a new content
281
- *
282
- * @param destination String destination id
283
- * @param content String content
284
- * @return RR this
285
- */
286
- replace: function(id, source) {
287
- $(id).replace(source);
288
- return this.highlight(id).rescan(id);
289
- },
290
-
291
- /**
292
- * removes the element by id
293
- *
294
- * @param String element id
295
- * @return RR this
296
- */
297
- remove: function(id) {
298
- if ($(id)) {
299
- $(id).remove(this.Options.removeFx);
300
- }
301
- },
302
-
303
- /**
304
- * Makes a remote form out of the form
305
- *
306
- * @param String form id
307
- * @return RR this
308
- */
309
- remotize_form: function(id) {
310
- var form = $(id);
311
- if (form) {
312
- form.remotize().enable()._.action += '.'+this.Options.format;
313
- }
314
- return this;
315
- },
316
-
317
- /**
318
- * Replaces the form with new content and makes it remote
319
- *
320
- * @param form id String form id
321
- * @param content String content
322
- * @return RR this
323
- */
324
- replace_form: function(id, source) {
325
- var form = $(id);
326
- if (form) {
327
- form.replace(source);
328
- this.remotize_form(id);
329
- }
330
-
331
- return this.rescan(id);
332
- },
333
-
334
- /**
335
- * Inserts the form source into the given element
336
- *
337
- * @param target id String target id
338
- * @param source String form source
339
- * @return RR this
340
- */
341
- show_form_for: function(id, source) {
342
- $(id).select('form').each('remove'); // removing old forms
343
- $(id).insert(source);
344
-
345
- return this.remotize_form($(id).first('form')).rescan(id);
346
- },
347
-
348
- /**
349
- * watches link clicks and processes the ajax edit/delete operations
350
- *
351
- * @param Event event
352
- */
353
- process_click: function(event) {
354
- var link;
355
-
356
- if ((link = event.find('a'+ this.Options.linkToAjaxEdit))) {
357
- event.stop();
358
- Xhr.load(link.get('href') + '.' + this.Options.format);
359
- } else if ((link = event.find('a'+ this.Options.linkToAjaxDelete)) && link.has('onclick')) {
360
- event.stop();
361
- eval('({f:'+ link.onclick.toString().replace('.submit', '.send')+'})').f.call(link);
362
- }
363
- },
364
-
365
- /**
366
- * Scans for updated elements
367
- *
368
- * @return RR this
369
- */
370
- rescan: function(scope) {
371
- $w('Draggable Droppable Tabs Slider Selectable').each(function(name) {
372
- if (name in window) {
373
- window[name].rescan(this.Options.rescanWithScopes ? scope : null);
374
- }
375
- }, this);
376
-
377
-
378
- return this;
379
- }
380
- };
381
56
 
382
-
383
- /**
384
- * the document onload hooks
385
- *
386
- * Copyright (C) 2010 Nikolay Nemshilov
387
- */
388
- $(document).on({
389
- ready: function() {
390
- RR.hide_flash();
391
- },
392
-
393
- click: function(event) {
394
- RR.process_click(event);
395
- }
396
- });
397
-
398
- window.RR = RR;
399
- })(window, document, RightJS);
57
+ })(window, document, RightJS);