puffer 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/.rspec +1 -0
  2. data/Gemfile +2 -1
  3. data/Gemfile.lock +12 -0
  4. data/VERSION +1 -1
  5. data/app/cells/puffer/base/additional.html.erb +17 -0
  6. data/app/cells/puffer/base_cell.rb +25 -0
  7. data/app/controllers/admin/dashboard_controller.rb +12 -0
  8. data/app/views/admin/dashboard/index.html.erb +1 -0
  9. data/app/views/layouts/puffer.html.erb +41 -23
  10. data/app/views/puffer/_form.html.erb +3 -11
  11. data/app/views/puffer/edit.html.erb +6 -4
  12. data/app/views/puffer/index.html.erb +11 -38
  13. data/app/views/puffer/new.html.erb +5 -2
  14. data/app/views/puffer/show.html.erb +4 -4
  15. data/config/routes.rb +7 -0
  16. data/lib/generators/puffer/controller/controller_generator.rb +1 -1
  17. data/lib/generators/puffer/install/templates/puffer/javascripts/rails-src.js +399 -0
  18. data/lib/generators/puffer/install/templates/puffer/javascripts/rails.js +14 -175
  19. data/lib/generators/puffer/install/templates/puffer/javascripts/right-calendar-src.js +1461 -0
  20. data/lib/generators/puffer/install/templates/puffer/javascripts/right-calendar.js +36 -0
  21. data/lib/generators/puffer/install/templates/puffer/javascripts/right-calendar.js.gz +0 -0
  22. data/lib/generators/puffer/install/templates/puffer/javascripts/right-in-edit-src.js +369 -0
  23. data/lib/generators/puffer/install/templates/puffer/javascripts/right-in-edit.js +13 -0
  24. data/lib/generators/puffer/install/templates/puffer/javascripts/right-in-edit.js.gz +0 -0
  25. data/lib/generators/puffer/install/templates/puffer/javascripts/right-lightbox-src.js +905 -0
  26. data/lib/generators/puffer/install/templates/puffer/javascripts/right-lightbox.js +24 -0
  27. data/lib/generators/puffer/install/templates/puffer/javascripts/right-lightbox.js.gz +0 -0
  28. data/lib/generators/puffer/install/templates/puffer/javascripts/right-sortable-src.js +428 -0
  29. data/lib/generators/puffer/install/templates/puffer/javascripts/right-sortable.js +17 -0
  30. data/lib/generators/puffer/install/templates/puffer/javascripts/right-sortable.js.gz +0 -0
  31. data/lib/generators/puffer/install/templates/puffer/javascripts/right-src.js +5892 -0
  32. data/lib/generators/puffer/install/templates/puffer/javascripts/right-tabs-src.js +1145 -0
  33. data/lib/generators/puffer/install/templates/puffer/javascripts/right-tabs.js +29 -0
  34. data/lib/generators/puffer/install/templates/puffer/javascripts/right-tabs.js.gz +0 -0
  35. data/lib/generators/puffer/install/templates/puffer/javascripts/right.js +95 -0
  36. data/lib/generators/puffer/install/templates/puffer/javascripts/right.js.gz +0 -0
  37. data/lib/generators/puffer/install/templates/puffer/stylesheets/puffer.css +168 -0
  38. data/lib/generators/puffer/install/templates/puffer/stylesheets/reset.css +60 -0
  39. data/lib/puffer.rb +2 -1
  40. data/lib/puffer/base.rb +15 -14
  41. data/lib/puffer/controller/config.rb +52 -16
  42. data/lib/puffer/controller/dsl.rb +27 -29
  43. data/lib/puffer/controller/helpers.rb +54 -9
  44. data/lib/puffer/controller/mutate.rb +4 -22
  45. data/lib/puffer/engine.rb +9 -0
  46. data/lib/puffer/extensions/controller.rb +5 -3
  47. data/lib/puffer/extensions/form.rb +14 -0
  48. data/lib/puffer/extensions/mapper.rb +36 -0
  49. data/lib/puffer/fields.rb +21 -0
  50. data/lib/puffer/fields/field.rb +8 -3
  51. data/lib/puffer/inputs.rb +23 -0
  52. data/lib/puffer/inputs/association.rb +11 -0
  53. data/lib/puffer/inputs/base.rb +39 -0
  54. data/lib/puffer/inputs/boolean.rb +19 -0
  55. data/lib/puffer/inputs/collection_association.rb +11 -0
  56. data/lib/puffer/inputs/date_time.rb +16 -0
  57. data/lib/puffer/inputs/file.rb +11 -0
  58. data/lib/puffer/inputs/password.rb +11 -0
  59. data/lib/puffer/inputs/select.rb +11 -0
  60. data/lib/puffer/inputs/text.rb +11 -0
  61. data/lib/puffer/path_set.rb +17 -0
  62. data/lib/puffer/resource.rb +14 -14
  63. data/puffer.gemspec +75 -8
  64. data/spec/dummy/app/controllers/admin/categories_controller.rb +4 -0
  65. data/spec/dummy/app/controllers/admin/posts_controller.rb +4 -0
  66. data/spec/dummy/app/controllers/admin/profiles_controller.rb +5 -0
  67. data/spec/dummy/app/controllers/admin/users_controller.rb +4 -0
  68. data/spec/dummy/app/models/friendship.rb +4 -0
  69. data/spec/dummy/app/models/profile.rb +2 -0
  70. data/spec/dummy/app/models/user.rb +7 -0
  71. data/spec/dummy/app/views/admin/users/index.html.erb +1 -0
  72. data/spec/dummy/config/puffer.rb +0 -0
  73. data/spec/dummy/db/migrate/20110107082706_create_friendships.rb +15 -0
  74. data/spec/dummy/db/schema.rb +9 -1
  75. data/spec/dummy/public/puffer/javascripts/rails-src.js +399 -0
  76. data/spec/dummy/public/puffer/javascripts/rails.js +14 -0
  77. data/spec/dummy/public/puffer/javascripts/right-calendar-src.js +1461 -0
  78. data/spec/dummy/public/puffer/javascripts/right-calendar.js +36 -0
  79. data/spec/dummy/public/puffer/javascripts/right-calendar.js.gz +0 -0
  80. data/spec/dummy/public/puffer/javascripts/right-in-edit-src.js +369 -0
  81. data/spec/dummy/public/puffer/javascripts/right-in-edit.js +13 -0
  82. data/spec/dummy/public/puffer/javascripts/right-in-edit.js.gz +0 -0
  83. data/spec/dummy/public/puffer/javascripts/right-lightbox-src.js +905 -0
  84. data/spec/dummy/public/puffer/javascripts/right-lightbox.js +24 -0
  85. data/spec/dummy/public/puffer/javascripts/right-lightbox.js.gz +0 -0
  86. data/spec/dummy/public/puffer/javascripts/right-sortable-src.js +428 -0
  87. data/spec/dummy/public/puffer/javascripts/right-sortable.js +17 -0
  88. data/spec/dummy/public/puffer/javascripts/right-sortable.js.gz +0 -0
  89. data/spec/dummy/public/puffer/javascripts/right-src.js +5892 -0
  90. data/spec/dummy/public/puffer/javascripts/right-tabs-src.js +1145 -0
  91. data/spec/dummy/public/puffer/javascripts/right-tabs.js +29 -0
  92. data/spec/dummy/public/puffer/javascripts/right-tabs.js.gz +0 -0
  93. data/spec/dummy/public/puffer/javascripts/right.js +95 -0
  94. data/spec/dummy/public/puffer/javascripts/right.js.gz +0 -0
  95. data/spec/dummy/public/puffer/stylesheets/puffer.css +168 -0
  96. data/spec/dummy/public/puffer/stylesheets/reset.css +60 -0
  97. data/spec/lib/fields_spec.rb +2 -0
  98. data/spec/lib/params_spec.rb +55 -54
  99. metadata +114 -23
  100. data/lib/generators/puffer/install/templates/puffer/javascripts/application.js +0 -2
  101. data/lib/generators/puffer/install/templates/puffer/javascripts/controls.js +0 -965
  102. data/lib/generators/puffer/install/templates/puffer/javascripts/dragdrop.js +0 -974
  103. data/lib/generators/puffer/install/templates/puffer/javascripts/effects.js +0 -1123
  104. data/lib/generators/puffer/install/templates/puffer/javascripts/prototype.js +0 -6001
  105. data/lib/puffer/railtie.rb +0 -5
data/.rspec CHANGED
@@ -1 +1,2 @@
1
1
  --colour
2
+ --format documentation
data/Gemfile CHANGED
@@ -2,6 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  gem 'rails', '~> 3.0.3'
4
4
  gem 'will_paginate', '~> 3.0.pre2'
5
+ gem 'cells', '~> 3.4.4'
5
6
 
6
7
  group :development, :test do
7
8
  gem "capybara", ">= 0.4.0"
@@ -11,7 +12,7 @@ group :development, :test do
11
12
  gem "autotest"
12
13
  gem 'forgery'
13
14
  gem 'fabrication'
14
-
15
15
  gem "jeweler"
16
+ gem "mongrel"
16
17
  end
17
18
 
data/Gemfile.lock CHANGED
@@ -43,16 +43,21 @@ GEM
43
43
  selenium-webdriver (>= 0.0.27)
44
44
  xpath (~> 0.1.2)
45
45
  celerity (0.8.6)
46
+ cells (3.4.4)
47
+ cgi_multipart_eof_fix (2.5.0)
46
48
  childprocess (0.1.6)
47
49
  ffi (~> 0.6.3)
48
50
  culerity (0.2.13)
51
+ daemons (1.1.0)
49
52
  diff-lcs (1.1.2)
50
53
  erubis (2.6.6)
51
54
  abstract (>= 1.0.0)
52
55
  fabrication (0.9.4)
56
+ fastthread (1.0.7)
53
57
  ffi (0.6.3)
54
58
  rake (>= 0.8.7)
55
59
  forgery (0.3.6)
60
+ gem_plugin (0.2.3)
56
61
  git (1.2.5)
57
62
  i18n (0.5.0)
58
63
  jeweler (1.5.2)
@@ -66,6 +71,11 @@ GEM
66
71
  mime-types (~> 1.16)
67
72
  treetop (~> 1.4.8)
68
73
  mime-types (1.16)
74
+ mongrel (1.1.5)
75
+ cgi_multipart_eof_fix (>= 2.4)
76
+ daemons (>= 1.0.3)
77
+ fastthread (>= 1.0.1)
78
+ gem_plugin (>= 0.2.3)
69
79
  nokogiri (1.4.4)
70
80
  polyglot (0.3.1)
71
81
  rack (1.2.1)
@@ -121,9 +131,11 @@ PLATFORMS
121
131
  DEPENDENCIES
122
132
  autotest
123
133
  capybara (>= 0.4.0)
134
+ cells (~> 3.4.4)
124
135
  fabrication
125
136
  forgery
126
137
  jeweler
138
+ mongrel
127
139
  rails (~> 3.0.3)
128
140
  rspec-rails
129
141
  sqlite3-ruby
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.0.6
@@ -0,0 +1,17 @@
1
+ <dl>
2
+ <dt>Links</dt>
3
+ <dd>
4
+ <%= link_to_unless_current t(:list, :target => resource.human), resource.index_path %>
5
+ <%= link_to_unless_current t(:add, :target => resource.human), resource.new_path %>
6
+ </dd>
7
+ <dt>Search</dt>
8
+ <dd>
9
+ <%= form_tag resource.index_path, {:method => :get} do %>
10
+ <%= text_field_tag :query, resource_session[:query] %>
11
+ <%= submit_tag 'Search' %>
12
+ <% if resource_session[:query].present? %>
13
+ <%= link_to 'clear', resource.index_path(:query => '') %>
14
+ <% end %>
15
+ <% end %>
16
+ </dd>
17
+ </dl>
@@ -0,0 +1,25 @@
1
+ module Puffer
2
+ class BaseCell < ::Cell::Rails
3
+
4
+ build do
5
+ self.class.to_s.gsub(/Controller$/, 'Cell').constantize rescue Puffer::BaseCell
6
+ end
7
+
8
+ helper_method :resource, :resource_session
9
+
10
+ def additional
11
+ render
12
+ end
13
+
14
+ private
15
+
16
+ def resource
17
+ parent_controller.resource
18
+ end
19
+
20
+ def resource_session
21
+ parent_controller.resource_session
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,12 @@
1
+ class Admin::DashboardController < ApplicationController
2
+ unloadable
3
+
4
+ layout 'puffer'
5
+
6
+ include Puffer::Controller::Helpers
7
+
8
+ def index
9
+ @title = 'Dashboard'
10
+ end
11
+
12
+ end
@@ -0,0 +1 @@
1
+ Dashboard!!!!!!!!!!
@@ -4,35 +4,53 @@
4
4
  <title><%= [@title, 'Puffer'].compact.join(' - ') %></title>
5
5
  <meta http-equiv="content-type" content="text/html;charset=utf-8" />
6
6
  <%= csrf_meta_tag %>
7
- <%= stylesheet_link_tag 'terbium_reset' %>
8
- <%= stylesheet_link_tag 'terbium' %>
7
+ <%= stylesheet_link_tag '/puffer/stylesheets/reset' %>
8
+ <%= stylesheet_link_tag '/puffer/stylesheets/puffer' %>
9
9
  <%= yield :stylesheet %>
10
- <%= javascript_include_tag 'prototype', 'effects', 'dragdrop', 'controls' %>
11
- <%= javascript_include_tag 'terbium' %>
12
- <%= javascript_include_tag 'terbium_popup' %>
10
+
11
+ <%= javascript_include_tag '/puffer/javascripts/right-src' %>
12
+ <%= javascript_include_tag '/puffer/javascripts/rails-src' %>
13
+ <%= javascript_include_tag '/puffer/javascripts/right-calendar-src' %>
13
14
  <%= yield :javascript %>
14
15
  </head>
15
16
  <body>
16
- <div class="header">
17
- <div class="logo"><a href="/admin">Puffer</a></div>
18
- <ul class="navigation">
19
- <%= puffer_navigation do |title, path| %>
20
- <li<%= raw(current_path.include?(path) ? ' class="selected"' : '') %>><%= link_to title, path %></li>
21
- <% end %>
22
- </ul>
23
- <div class="logout">
24
- </div>
25
- </div>
26
- <div class="additional_navigation" id="additional_navigation">
27
- <%= yield :additional_navigation %>
28
- </div>
29
17
  <div class="body">
30
- <div class="content">
31
- <%= yield %>
18
+ <div class="header">
19
+ <div class="logo">
20
+ <%= link_to 'Puffer', admin_root_path %>
21
+ </div>
22
+ <ul class="navigation">
23
+ <% puffer_navigation do |title, path, current| %>
24
+ <li<%= raw(current ? ' class="selected"' : '') %>><%= link_to title, path %></li>
25
+ <% end %>
26
+ </ul>
27
+ <div class="logout">
28
+ </div>
29
+ </div>
30
+ <div class="columns">
31
+ <div class="column sidebar">
32
+ <div class="padder">
33
+ <ul class="navigation">
34
+ <% sidebar_puffer_navigation do |title, path, current| %>
35
+ <li<%= raw(current ? ' class="selected"' : '') %>>
36
+ <%= link_to title, path %>
37
+ <% if current && (additional = render_cell('puffer/base', 'additional')).present? %>
38
+ <div class="additional">
39
+ <%= additional %>
40
+ </div>
41
+ <% end %>
42
+ </li>
43
+ <% end %>
44
+ </ul>
45
+ <%= yield :sidebar %>
46
+ </div>
47
+ </div>
48
+ <div class="column">
49
+ <div class="content">
50
+ <%= yield %>
51
+ </div>
52
+ </div>
32
53
  </div>
33
54
  </div>
34
- <% if flash[:notice] %>
35
- <div class="notice" id="notice"><%= flash[:notice] %></div>
36
- <% end %>
37
55
  </body>
38
56
  </html>
@@ -1,19 +1,11 @@
1
- <%= f.error_messages %>
2
-
3
1
  <ul>
4
- <% case action
5
- when 'new' then
6
- @fields = controller.create_fields || controller.form_fields
7
- when 'edit' then
8
- @fields = controller.update_fields || controller.form_fields
9
- end %>
10
- <% @fields.each do |field| -%>
11
- <li><%= f.terbium_field field %></li>
2
+ <% (action == 'new' ? create_fields : update_fields).each do |field| -%>
3
+ <li><%= f.puffer_field field %></li>
12
4
  <% end -%>
13
5
  </ul>
14
6
 
15
7
  <div class="buttons">
16
8
  <%= f.submit 'Save' %>
17
9
  <%= f.submit 'Save and exit' %>
18
- <%= link_to 'cancel', request.referer %>
10
+ <%= link_to 'cancel', (request.referer || resource.index_path) %>
19
11
  </div>
@@ -1,10 +1,11 @@
1
- <% @title = "Edit #{controller.model_name.humanize.downcase}" %>
1
+ <% @title = "Edit #{resource.human}" %>
2
2
  <h1><%= @title %></h1>
3
- <% form_for record, :url => resource_path, :html => {:multipart => true} do |f| %>
4
- <%= hidden_field_tag :edit_locale, params[:edit_locale] if params[:edit_locale] %>
5
-
3
+ <%= form_for record, :url => resource.path, :html => {:multipart => true} do |f| %>
6
4
  <%= render :partial => 'terbium/form', :locals => { :f => f, :action => 'edit' } %>
7
5
  <% end %>
6
+
7
+
8
+ <% if false %>
8
9
  <% content_for :additional_navigation do %>
9
10
  <ul class="buttons">
10
11
  <% 0.upto(resource_ancestors_records.length - 1) do |i| %>
@@ -18,3 +19,4 @@
18
19
  <% end %>
19
20
  </ul>
20
21
  <% end %>
22
+ <% end %>
@@ -1,13 +1,11 @@
1
- <% @title = current_resource.human %>
1
+ <% @title = resource.human %>
2
2
  <h1><%= @title %></h1>
3
- <% if respond_to? :will_paginate %>
4
- <%= will_paginate @records, :url => current_resource.index_path(:page => '') %>
5
- <% end %>
3
+ <%= will_paginate @records, :url => resource.index_path(:page => '') %>
6
4
  <div class="columns">
7
5
  <div class="row">
8
6
  <div class="column left_column">
9
7
  <% if @records.empty? %>
10
- <p>Sorry, but there is no records in <%= current_resource.human %></p>
8
+ <p>Sorry, but there is no records in <%= resource.human %></p>
11
9
  <% else %>
12
10
  <table class="list_table">
13
11
  <thead>
@@ -25,10 +23,10 @@
25
23
  <td><%= render_field record, field %></td>
26
24
  <% end -%>
27
25
  <td class="actions">
28
- <%= link_to 'show', current_resource.path(record), :class => 'show_entry' if controller.show_fields || resource_children.present? %>
29
- <%= link_to 'edit', current_resource.edit_path(record), :class => 'edit_entry' if controller.update_fields || controller.form_fields %>
30
- <%= link_to 'destroy', current_resource.path(record), :confirm => "Are you sure?", :method => :delete, :class => 'remove_entry' if current_config.destroy %>
31
- <% current_resource.children(:post_id => record.id).each do |child| %>
26
+ <%= link_to 'show', resource.path(record), :class => 'show_entry' if controller.show_fields || resource_children.present? %>
27
+ <%= link_to 'edit', resource.edit_path(record), :class => 'edit_entry' if controller.update_fields || controller.form_fields %>
28
+ <%= link_to 'destroy', resource.path(record), :confirm => "Are you sure?", :method => :delete, :class => 'remove_entry' if configuration.destroy %>
29
+ <% resource.children(:post_id => record.id).each do |child| %>
32
30
  <% if child.plural? %>
33
31
  <p><%= link_to "#{child.human}(#{child.collection.size})", child.index_path %></p>
34
32
  <% else %>
@@ -42,42 +40,17 @@
42
40
  </table>
43
41
  <% end %>
44
42
  </div>
45
- <div class="column right_column panel">
46
- <dl>
47
- <dt>Search</dt>
48
- <dd>
49
- <%= form_tag current_resource.index_path, {:method => :get} do %>
50
- <%= text_field_tag :query, resource_session[:query] %>
51
- <%= submit_tag 'Search' %>
52
- <% if resource_session[:query].present? %>
53
- <%= link_to 'clear', current_resource.index_path(:query => '') %>
54
- <% end %>
55
- <% end %>
56
- </dd>
57
- <% index_fields.boolean.each do |field| %>
58
- <dt><%= field.label %></dt>
59
- <dd>
60
- <ul>
61
- <% [['All', nil], ['Yes', '1'], ['No', '0'], ['Not set', 'nil']].each do |option| %>
62
- <li><%= link_to_unless resource_session[:boolean][field.query_column] == option[1], option[0], current_resource.index_path(:boolean => { field.query_column => option[1]}) %></li>
63
- <% end %>
64
- </ul>
65
- </dd>
66
- <% end %>
67
- </dl>
68
- </div>
69
43
  </div>
70
44
  </div>
71
- <% if respond_to? :will_paginate %>
72
- <%= will_paginate @records, :url => current_resource.index_path(:page => '') %>
73
- <% end %>
45
+ <%= will_paginate @records, :url => resource.index_path(:page => '') %>
74
46
 
75
47
  <% content_for :additional_navigation do %>
76
48
  <ul class="buttons">
77
- <% current_resource.ancestors.each do |resource| %>
49
+ <% resource.ancestors.each do |resource| %>
78
50
  <%= link_to resource.plural? ? resource.human : resource.member.to_title, resource.index_path %>
79
51
  <%= link_to resource.member.to_title, resource.path unless resource.plural? %>
80
52
  <% end %>
81
- <%= link_to current_resource.plural? ? current_resource.human : current_resource.member.to_title, current_resource.index_path %>
53
+ <%= link_to resource.plural? ? resource.human : resource.member.to_title, resource.index_path %>
82
54
  </ul>
83
55
  <% end %>
56
+
@@ -1,8 +1,10 @@
1
- <% @title = "New #{controller.model_name.humanize.downcase}" %>
1
+ <% @title = "New #{resource.human}" %>
2
2
  <h1><%= @title %></h1>
3
- <% form_for record, :url => resource_path, :html => {:multipart => true} do |f| %>
3
+ <%= form_for record, :url => resource.index_path, :html => {:multipart => true} do |f| %>
4
4
  <%= render :partial => 'terbium/form', :locals => { :f => f, :action => 'new' } %>
5
5
  <% end %>
6
+
7
+ <% if false %>
6
8
  <% content_for :additional_navigation do %>
7
9
  <ul class="buttons">
8
10
  <% 0.upto(resource_ancestors_records.length - 1) do |i| %>
@@ -16,3 +18,4 @@
16
18
  <% end %>
17
19
  </ul>
18
20
  <% end %>
21
+ <% end %>
@@ -1,10 +1,10 @@
1
- <% @title = "Show #{current_resource.human}" %>
1
+ <% @title = "Show #{resource.human}" %>
2
2
  <h1><%= @title %></h1>
3
3
  <ul class="show_entry">
4
- <% if current_resource.children.present? %>
4
+ <% if resource.children.present? %>
5
5
  <li>
6
6
  <h2>Associations</h2>
7
- <% current_resource.children.each do |child| %>
7
+ <% resource.children.each do |child| %>
8
8
  <%= link_to child.human, child.index_path %>
9
9
  <% end %>
10
10
  </li>
@@ -21,7 +21,7 @@
21
21
  </ul>
22
22
  <% content_for :additional_navigation do %>
23
23
  <ul class="buttons">
24
- <% (current_resource.ancestors + [current_resource]).each do |resource| %>
24
+ <% (resource.ancestors + [resource]).each do |resource| %>
25
25
  <%= link_to resource.plural? ? resource.human : resource.member.to_title, resource.index_path %>
26
26
  <%= link_to resource.member.to_title, resource.path unless resource.plural? %>
27
27
  <% end %>
data/config/routes.rb ADDED
@@ -0,0 +1,7 @@
1
+ Rails.application.routes.draw do
2
+
3
+ namespace :admin do
4
+ root :to => 'dashboard#index'
5
+ end
6
+
7
+ end
@@ -11,7 +11,7 @@ class Puffer::ControllerGenerator < Rails::Generators::NamedBase
11
11
  private
12
12
 
13
13
  def controller_name
14
- ((@modules.presence || ['Admin']) << @model_name.pluralize).join('::')
14
+ [(swallow_nil{@modules.first} || 'Admin'), @model_name.pluralize].join('::')
15
15
  end
16
16
 
17
17
  def attributes
@@ -0,0 +1,399 @@
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
+ (function(window, document, RightJS) {
8
+ /**
9
+ * Underscored aliases for Ruby On Rails
10
+ *
11
+ * Copyright (C) 2009-2010 Nikolay Nemshilov
12
+ */
13
+
14
+ /**
15
+ * The Rails plugin initialization script
16
+ *
17
+ * Copyright (C) 2010 Nikolay Nemshilov
18
+ */
19
+
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
+ }
70
+ }
71
+ });
72
+
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) {
113
+ return Object.merge({
114
+ onCreate: function() { element.fire('ajax:loading', this); },
115
+ onComplete: function() { element.fire('ajax:complete', this); },
116
+ onSuccess: function() { element.fire('ajax:success', this); },
117
+ onFailure: function() { element.fire('ajax:failure', this); }
118
+ }, options);
119
+ };
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
+
131
+ if (remote) {
132
+ Xhr.load(url, add_xhr_events(link, {
133
+ method: method || 'get',
134
+ spinner: link.get('data-spinner')
135
+ }));
136
+
137
+ } else if (method) {
138
+ var param = $$('meta[name=csrf-param]')[0],
139
+ token = $$('meta[name=csrf-token]')[0],
140
+ form = $E('form', {action: url, method: 'post'});
141
+
142
+ if (param && token) {
143
+ form.insert('<input type="hidden" name="'+param.get('content')+'" value="'+token.get('content')+'" />');
144
+ }
145
+
146
+ form.insert('<input type="hidden" name="_method" value="'+method+'"/>')
147
+ .insertTo(document.body).submit();
148
+ }
149
+ };
150
+
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
+ }
168
+ }
169
+ });
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
+
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);