magic_grid 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +19 -0
  3. data/Rakefile +44 -0
  4. data/lib/assets/javascripts/magic_grid.js +154 -0
  5. data/lib/locales/en.yml +7 -0
  6. data/lib/magic_grid.rb +7 -0
  7. data/lib/magic_grid/definition.rb +212 -0
  8. data/lib/magic_grid/engine.rb +14 -0
  9. data/lib/magic_grid/helpers.rb +243 -0
  10. data/lib/magic_grid/railtie.rb +9 -0
  11. data/lib/magic_grid/version.rb +3 -0
  12. data/lib/tasks/magic_grid_tasks.rake +4 -0
  13. data/test/dummy/Rakefile +7 -0
  14. data/test/dummy/app/assets/javascripts/application.js +9 -0
  15. data/test/dummy/app/assets/javascripts/posts.js +8 -0
  16. data/test/dummy/app/assets/javascripts/users.js +2 -0
  17. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  18. data/test/dummy/app/assets/stylesheets/posts.css +4 -0
  19. data/test/dummy/app/assets/stylesheets/scaffold.css +56 -0
  20. data/test/dummy/app/assets/stylesheets/users.css +4 -0
  21. data/test/dummy/app/controllers/application_controller.rb +3 -0
  22. data/test/dummy/app/controllers/posts_controller.rb +104 -0
  23. data/test/dummy/app/controllers/users_controller.rb +84 -0
  24. data/test/dummy/app/helpers/application_helper.rb +2 -0
  25. data/test/dummy/app/helpers/posts_helper.rb +2 -0
  26. data/test/dummy/app/helpers/users_helper.rb +2 -0
  27. data/test/dummy/app/models/post.rb +6 -0
  28. data/test/dummy/app/models/user.rb +4 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +19 -0
  30. data/test/dummy/app/views/posts/_form.html.erb +33 -0
  31. data/test/dummy/app/views/posts/by_user.html.erb +18 -0
  32. data/test/dummy/app/views/posts/edit.html.erb +6 -0
  33. data/test/dummy/app/views/posts/index.html.erb +76 -0
  34. data/test/dummy/app/views/posts/new.html.erb +5 -0
  35. data/test/dummy/app/views/posts/show.html.erb +25 -0
  36. data/test/dummy/app/views/users/_form.html.erb +25 -0
  37. data/test/dummy/app/views/users/edit.html.erb +6 -0
  38. data/test/dummy/app/views/users/index.html.erb +42 -0
  39. data/test/dummy/app/views/users/new.html.erb +5 -0
  40. data/test/dummy/app/views/users/show.html.erb +15 -0
  41. data/test/dummy/config.ru +4 -0
  42. data/test/dummy/config/application.rb +59 -0
  43. data/test/dummy/config/boot.rb +10 -0
  44. data/test/dummy/config/database.yml +25 -0
  45. data/test/dummy/config/environment.rb +5 -0
  46. data/test/dummy/config/environments/development.rb +40 -0
  47. data/test/dummy/config/environments/production.rb +67 -0
  48. data/test/dummy/config/environments/test.rb +37 -0
  49. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  50. data/test/dummy/config/initializers/inflections.rb +15 -0
  51. data/test/dummy/config/initializers/mime_types.rb +5 -0
  52. data/test/dummy/config/initializers/secret_token.rb +7 -0
  53. data/test/dummy/config/initializers/session_store.rb +8 -0
  54. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  55. data/test/dummy/config/locales/en.yml +5 -0
  56. data/test/dummy/config/routes.rb +68 -0
  57. data/test/dummy/db/development.sqlite3 +0 -0
  58. data/test/dummy/db/migrate/20111025191809_create_users.rb +10 -0
  59. data/test/dummy/db/migrate/20111025195229_create_posts.rb +13 -0
  60. data/test/dummy/db/schema.rb +34 -0
  61. data/test/dummy/db/test.sqlite3 +0 -0
  62. data/test/dummy/log/development.log +49565 -0
  63. data/test/dummy/log/passenger.3000.log +362 -0
  64. data/test/dummy/log/test.log +77 -0
  65. data/test/dummy/public/404.html +26 -0
  66. data/test/dummy/public/422.html +26 -0
  67. data/test/dummy/public/500.html +26 -0
  68. data/test/dummy/public/css/smoothness/images/ui-bg_flat_0_aaaaaa_40x100.png +0 -0
  69. data/test/dummy/public/css/smoothness/images/ui-bg_flat_75_ffffff_40x100.png +0 -0
  70. data/test/dummy/public/css/smoothness/images/ui-bg_glass_55_fbf9ee_1x400.png +0 -0
  71. data/test/dummy/public/css/smoothness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  72. data/test/dummy/public/css/smoothness/images/ui-bg_glass_75_dadada_1x400.png +0 -0
  73. data/test/dummy/public/css/smoothness/images/ui-bg_glass_75_e6e6e6_1x400.png +0 -0
  74. data/test/dummy/public/css/smoothness/images/ui-bg_glass_95_fef1ec_1x400.png +0 -0
  75. data/test/dummy/public/css/smoothness/images/ui-bg_highlight-soft_75_cccccc_1x100.png +0 -0
  76. data/test/dummy/public/css/smoothness/images/ui-icons_222222_256x240.png +0 -0
  77. data/test/dummy/public/css/smoothness/images/ui-icons_2e83ff_256x240.png +0 -0
  78. data/test/dummy/public/css/smoothness/images/ui-icons_454545_256x240.png +0 -0
  79. data/test/dummy/public/css/smoothness/images/ui-icons_888888_256x240.png +0 -0
  80. data/test/dummy/public/css/smoothness/images/ui-icons_cd0a0a_256x240.png +0 -0
  81. data/test/dummy/public/css/smoothness/jquery-ui-1.8.18.custom.css +565 -0
  82. data/test/dummy/public/favicon.ico +0 -0
  83. data/test/dummy/script/rails +6 -0
  84. data/test/dummy/test/fixtures/posts.yml +23 -0
  85. data/test/dummy/test/fixtures/users.yml +15 -0
  86. data/test/dummy/test/functional/posts_controller_test.rb +49 -0
  87. data/test/dummy/test/functional/users_controller_test.rb +50 -0
  88. data/test/dummy/test/integration/spider_test.rb +27 -0
  89. data/test/dummy/test/unit/helpers/posts_helper_test.rb +4 -0
  90. data/test/dummy/test/unit/helpers/users_helper_test.rb +4 -0
  91. data/test/dummy/test/unit/post_test.rb +7 -0
  92. data/test/dummy/test/unit/user_test.rb +7 -0
  93. data/test/magic_grid_test.rb +11 -0
  94. data/test/test_helper.rb +17 -0
  95. metadata +333 -0
@@ -0,0 +1,84 @@
1
+ class UsersController < ApplicationController
2
+ # GET /users
3
+ # GET /users.json
4
+ def index
5
+ @basic_users = User.includes(:posts)
6
+ @optimized_users = User.joins(:posts).select("users.*, count(*) as post_count").group('posts.user_id')
7
+
8
+ respond_to do |format|
9
+ format.html # index.html.erb
10
+ format.json { render json: @users }
11
+ end
12
+ end
13
+
14
+ # GET /users/1
15
+ # GET /users/1.json
16
+ def show
17
+ @user = User.find(params[:id])
18
+
19
+ respond_to do |format|
20
+ format.html # show.html.erb
21
+ format.json { render json: @user }
22
+ end
23
+ end
24
+
25
+ # GET /users/new
26
+ # GET /users/new.json
27
+ def new
28
+ @user = User.new
29
+
30
+ respond_to do |format|
31
+ format.html # new.html.erb
32
+ format.json { render json: @user }
33
+ end
34
+ end
35
+
36
+ # GET /users/1/edit
37
+ def edit
38
+ @user = User.find(params[:id])
39
+ end
40
+
41
+ # POST /users
42
+ # POST /users.json
43
+ def create
44
+ @user = User.new(params[:user])
45
+
46
+ respond_to do |format|
47
+ if @user.save
48
+ format.html { redirect_to @user, notice: 'User was successfully created.' }
49
+ format.json { render json: @user, status: :created, location: @user }
50
+ else
51
+ format.html { render action: "new" }
52
+ format.json { render json: @user.errors, status: :unprocessable_entity }
53
+ end
54
+ end
55
+ end
56
+
57
+ # PUT /users/1
58
+ # PUT /users/1.json
59
+ def update
60
+ @user = User.find(params[:id])
61
+
62
+ respond_to do |format|
63
+ if @user.update_attributes(params[:user])
64
+ format.html { redirect_to @user, notice: 'User was successfully updated.' }
65
+ format.json { head :ok }
66
+ else
67
+ format.html { render action: "edit" }
68
+ format.json { render json: @user.errors, status: :unprocessable_entity }
69
+ end
70
+ end
71
+ end
72
+
73
+ # DELETE /users/1
74
+ # DELETE /users/1.json
75
+ def destroy
76
+ @user = User.find(params[:id])
77
+ @user.destroy
78
+
79
+ respond_to do |format|
80
+ format.html { redirect_to users_url }
81
+ format.json { head :ok }
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module PostsHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ module UsersHelper
2
+ end
@@ -0,0 +1,6 @@
1
+ class Post < ActiveRecord::Base
2
+ belongs_to :user
3
+ def self.search(q)
4
+ return where('posts.title LIKE :q OR posts.body LIKE :q', {:q => "%#{q}%"})
5
+ end
6
+ end
@@ -0,0 +1,4 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :posts
3
+ def to_s() name end
4
+ end
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application", "/css/smoothness/jquery-ui-1.8.18.custom.css" %>
6
+ <%= javascript_include_tag "application", "magic_grid" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <p>
11
+ <%= link_to "Posts", posts_path %>
12
+ <%= link_to "Users", users_path %>
13
+ </p>
14
+ <hr />
15
+
16
+ <%= yield %>
17
+
18
+ </body>
19
+ </html>
@@ -0,0 +1,33 @@
1
+ <%= form_for(@post) do |f| %>
2
+ <% if @post.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @post.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :title %><br />
16
+ <%= f.text_field :title %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :body %><br />
20
+ <%= f.text_area :body %>
21
+ </div>
22
+ <div class="field">
23
+ <%= f.label :published %><br />
24
+ <%= f.check_box :published %>
25
+ </div>
26
+ <div class="field">
27
+ <%= f.label :user_id %><br />
28
+ <%= f.collection_select :user_id, User.all, :id, :name %>
29
+ </div>
30
+ <div class="actions">
31
+ <%= f.submit %>
32
+ </div>
33
+ <% end %>
@@ -0,0 +1,18 @@
1
+ <h1><%= @title || "Listing posts" %></h1>
2
+
3
+ <%= magic_grid(@posts,
4
+ [ :title,
5
+ {:col => :body, :to_s => Proc.new {|r| r.body.truncate(30)} },
6
+ :published,
7
+ :user,
8
+ { :label => 'Actions',
9
+ :to_s => Proc.new { |post| %>
10
+ <%= link_to 'Show', post %>
11
+ <%= link_to 'Edit', edit_post_path(post) %>
12
+ <%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>
13
+ <% }
14
+ }
15
+ ],
16
+ :searchable => [:body, 'users.name'],
17
+ :per_page => 5,
18
+ :id => 2, :remote => true) %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing post</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @post %> |
6
+ <%= link_to 'Back', posts_path %>
@@ -0,0 +1,76 @@
1
+ <h1><%= @title || "Listing posts" %></h1>
2
+
3
+ <h3>Empty Collection</h3>
4
+ <%= magic_grid(@some_array, ["Name", "E-mail Address", "Website"],
5
+ :searchable => true,
6
+ :post_filter => Proc.new {|c| Rails.logger.debug "Final filter!"; c}) %>
7
+
8
+ <h3>External listener</h3>
9
+ <%= select nil, :user_id,
10
+ options_from_collection_for_select(User.all, :id, :name, params[:user_id]),
11
+ {:prompt => true}, :id => :user_id %>
12
+ <%= magic_grid(@posts,
13
+ [ :title,
14
+ {:col => :body, :to_s => Proc.new {|r| r.body.truncate(30)} },
15
+ :published,
16
+ :user,
17
+ { :label => 'Actions',
18
+ :to_s => Proc.new { |post| %>
19
+ <%= link_to 'Show', post %>
20
+ <%= link_to 'Edit', edit_post_path(post) %>
21
+ <%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>
22
+ <% }
23
+ }
24
+ ],
25
+ :searchable => [:body, 'users.name'],
26
+ :live_search => 2000,
27
+ :search_method => false,
28
+ :min_search_length => 2,
29
+ :per_page => 5,
30
+ :listeners => {
31
+ :user_id => :user_id
32
+ },
33
+ :searcher_size => 42,
34
+ :id => 1,
35
+ :remote => true,
36
+ ) %>
37
+
38
+ <h3>Basic grid with built-in search</h3>
39
+ <%= magic_grid(@posts,
40
+ [ :title,
41
+ {:col => :body, :to_s => Proc.new {|r| r.body.truncate(30)} },
42
+ :published,
43
+ :user,
44
+ { :label => 'Actions',
45
+ :to_s => Proc.new { |post| %>
46
+ <%= link_to 'Show', post %>
47
+ <%= link_to 'Edit', edit_post_path(post) %>
48
+ <%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>
49
+ <% }
50
+ }
51
+ ],
52
+ :searchable => [:body, 'users.name'],
53
+ :per_page => 5,
54
+ :id => 2, :remote => true) %>
55
+
56
+ <h3>External search field</h3>
57
+ <%= search_field_tag :posts_search %>
58
+ <%= magic_grid @posts, [:title, :body, :published, :user, 'Actions'],
59
+ :searchable => [:body, 'users.name'],
60
+ :searcher => :posts_search, :live_search => true,
61
+ :search_method => false,
62
+ :id => 3, :remote => true do |post| %>
63
+ <tr>
64
+ <td><%= post.title %></td>
65
+ <td><%= post.body.truncate(30) %></td>
66
+ <td><%= post.published %></td>
67
+ <td><%= link_to post.user, post.user %></td>
68
+ <td>
69
+ <%= link_to 'Show', post %>
70
+ <%= link_to 'Edit', edit_post_path(post) %>
71
+ <%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %>
72
+ </td>
73
+ </tr>
74
+ <% end %>
75
+
76
+ <%= link_to 'New Post', new_post_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New post</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', posts_path %>
@@ -0,0 +1,25 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <b>Title:</b>
5
+ <%= @post.title %>
6
+ </p>
7
+
8
+ <p>
9
+ <b>Body:</b>
10
+ <%= @post.body %>
11
+ </p>
12
+
13
+ <p>
14
+ <b>Published:</b>
15
+ <%= @post.published %>
16
+ </p>
17
+
18
+ <p>
19
+ <b>User:</b>
20
+ <%= @post.user %>
21
+ </p>
22
+
23
+
24
+ <%= link_to 'Edit', edit_post_path(@post) %> |
25
+ <%= link_to 'Back', posts_path %>
@@ -0,0 +1,25 @@
1
+ <%= form_for(@user) do |f| %>
2
+ <% if @user.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @user.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :name %><br />
16
+ <%= f.text_field :name %>
17
+ </div>
18
+ <div class="field">
19
+ <%= f.label :email %><br />
20
+ <%= f.text_field :email %>
21
+ </div>
22
+ <div class="actions">
23
+ <%= f.submit %>
24
+ </div>
25
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing user</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @user %> |
6
+ <%= link_to 'Back', users_path %>
@@ -0,0 +1,42 @@
1
+ <h1>Listing users</h1>
2
+
3
+ <%= magic_grid(@basic_users, [:name, :email, :posts, 'Actions'],
4
+ :per_page => 10, :remote => true,
5
+ :top_pager => false, :bottom_pager => true) do |user| %>
6
+ <tr>
7
+ <td><%= user.name %></td>
8
+ <td><%= mail_to user.email %></td>
9
+ <td><%= link_to user.posts.count, "/posts/by-user/#{user.id}" %></td>
10
+ <td>
11
+ <%= link_to 'Show', user %>
12
+ <%= link_to 'Edit', edit_user_path(user) %>
13
+ <%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %>
14
+ </td>
15
+ </tr>
16
+ <% end %>
17
+
18
+ <%= magic_grid( Array(1..1000), ["Number", "Word"], :per_page => 5) do |n| %>
19
+ <tr>
20
+ <td><%= n %></td>
21
+ <td><%= n.ordinalize %></td>
22
+ </tr>
23
+ <% end %>
24
+
25
+ <%= magic_grid(@optimized_users,
26
+ [{:sql => 'users.name', :label => 'Name'}, :email, :posts, 'Actions'],
27
+ :per_page => false,
28
+ :searchable => [0, 1]) do |user| %>
29
+ <tr>
30
+ <td><%= user.name %></td>
31
+ <td><%= mail_to user.email %></td>
32
+ <td><%= link_to user.post_count, "/posts/by-user/#{user.id}" %></td>
33
+ <td>
34
+ <%= link_to 'Show', user %>
35
+ <%= link_to 'Edit', edit_user_path(user) %>
36
+ <%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %>
37
+ </td>
38
+ </tr>
39
+ <% end %>
40
+ <br />
41
+
42
+ <%= link_to 'New User', new_user_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New user</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', users_path %>
@@ -0,0 +1,15 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <b>Name:</b>
5
+ <%= @user.name %>
6
+ </p>
7
+
8
+ <p>
9
+ <b>Email:</b>
10
+ <%= @user.email %>
11
+ </p>
12
+
13
+
14
+ <%= link_to 'Edit', edit_user_path(@user) %> |
15
+ <%= link_to 'Back', users_path %>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,59 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ if defined?(Bundler)
6
+ # If you precompile assets before deploying to production, use this line
7
+ Bundler.require(*Rails.groups(:assets => %w(development test)))
8
+ # If you want your assets lazily compiled in production, use this line
9
+ # Bundler.require(:default, :assets, Rails.env)
10
+ end
11
+
12
+ module Dummy
13
+ class Application < Rails::Application
14
+ # Settings in config/environments/* take precedence over those specified here.
15
+ # Application configuration should go into files in config/initializers
16
+ # -- all .rb files in that directory are automatically loaded.
17
+
18
+ # Custom directories with classes and modules you want to be autoloadable.
19
+ # config.autoload_paths += %W(#{config.root}/extras)
20
+
21
+ # Only load the plugins named here, in the order given (default is alphabetical).
22
+ # :all can be used as a placeholder for all plugins not explicitly named.
23
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24
+
25
+ # Activate observers that should always be running.
26
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
27
+
28
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
30
+ # config.time_zone = 'Central Time (US & Canada)'
31
+
32
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
33
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
34
+ # config.i18n.default_locale = :de
35
+
36
+ # Configure the default encoding used in templates for Ruby 1.9.
37
+ config.encoding = "utf-8"
38
+
39
+ # Configure sensitive parameters which will be filtered from the log file.
40
+ config.filter_parameters += [:password]
41
+
42
+ # Use SQL instead of Active Record's schema dumper when creating the database.
43
+ # This is necessary if your schema can't be completely dumped by the schema dumper,
44
+ # like if you have constraints or database-specific column types
45
+ # config.active_record.schema_format = :sql
46
+
47
+ # Enforce whitelist mode for mass assignment.
48
+ # This will create an empty whitelist of attributes available for mass-assignment for all models
49
+ # in your app. As such, your models will need to explicitly whitelist or blacklist accessible
50
+ # parameters by using an attr_accessible or attr_protected declaration.
51
+ # config.active_record.whitelist_attributes = true
52
+
53
+ # Enable the asset pipeline
54
+ config.assets.enabled = true
55
+
56
+ # Version of your assets, change this if you want to expire all your assets
57
+ config.assets.version = '1.0'
58
+ end
59
+ end