admin_views 0.1 → 0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,20 @@
1
1
  class Admin::<%= pluralize(model_class) %>Controller < Admin::AdminController
2
+ helper_method :order_param
3
+
2
4
  # GET /admin/<%= model_plural %>
3
5
  def index
4
6
  if params[:search]
5
- # TODO: By default search all text fields
6
- @<%= model_plural %> = <%= model_class %>.paginate(:all, :page => params[:page])
7
+ sql, sql_params = [], []
8
+ # By default searches all text fields
9
+ <% search_cols = columns.find_all {|col| [:text, :string].include?(col.type) }.collect {|col| "'#{col.name}'" }.join(",") -%>
10
+ [<%= search_cols %>].each do |col|
11
+ sql << "#{col} LIKE ?"
12
+ sql_params << "%#{params[:search]}%"
13
+ end
14
+ conditions = [sql.join(" OR "), sql_params].flatten
15
+ @<%= model_plural %> = <%= model_class %>.paginate(:all, :page => params[:page], :conditions => conditions, :order => order_sql(params))
7
16
  else
8
- @<%= model_plural %> = <%= model_class %>.paginate(:all, :page => params[:page])
17
+ @<%= model_plural %> = <%= model_class %>.paginate(:all, :page => params[:page], :order => order_sql(params))
9
18
  end
10
19
  end
11
20
 
@@ -53,4 +62,26 @@ class Admin::<%= pluralize(model_class) %>Controller < Admin::AdminController
53
62
 
54
63
  redirect_to(admin_<%= model_plural %>_url)
55
64
  end
65
+
66
+ private
67
+ def order_sql(params)
68
+ if params[:order]
69
+ split = params[:order].split("_")
70
+ order = split.last.upcase
71
+ column = split[0..-2].join("_")
72
+ "#{column} #{order}"
73
+ end
74
+ end
75
+
76
+ def order_param(col_name)
77
+ order = params[:order]
78
+
79
+ if order == "#{col_name}_asc"
80
+ "#{col_name}_asc"
81
+ elsif order == "#{col_name}_desc"
82
+ "#{col_name}_desc"
83
+ else
84
+ "#{col_name}_asc"
85
+ end
86
+ end
56
87
  end
@@ -3,18 +3,31 @@
3
3
  </div>
4
4
 
5
5
  <div id="search">
6
-
6
+ <form method="get">
7
+ <%%= text_field_tag 'search', params[:search] %>
8
+ <%%= submit_tag "Search" %>
9
+ </form>
7
10
  </div>
8
11
 
9
12
  <h2><%= model_class %></h2>
10
13
  <table>
11
14
  <tr>
12
- <% for col in columns -%>
13
- <th><%= col.human_name %></th>
15
+ <% columns.each do |col| -%>
16
+ <th><%%= link_to '<%= col.human_name %>', params.merge(:order => order_param('<%= col.name %>')) %></th>
14
17
  <% end -%>
15
18
  <th></th>
16
19
  </tr>
17
- <%%= render @<%= model_plural %> %>
20
+ <%% @<%= model_plural %>.each do |<%= model_name %>| %>
21
+ <tr id="<%= model_name %>_<%%= <%= model_name %>.id %>">
22
+ <% columns.each do |col| -%>
23
+ <td><%%= h <%= model_name %>.<%= col.name %> %></td>
24
+ <% end -%>
25
+ <td>
26
+ <%%= link_to "Edit", edit_admin_<%= model_name %>_path(<%= model_name %>) %>
27
+ <%%= link_to "Delete", admin_<%= model_name %>_path(<%= model_name %>), :method => :delete, :confirm => "Are you sure?" %>
28
+ </td>
29
+ </tr>
30
+ <%% end -%>
18
31
  </table>
19
32
 
20
33
  <%%= will_paginate @<%= model_plural %> %>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: admin_views
3
3
  version: !ruby/object:Gem::Version
4
- version: "0.1"
4
+ version: "0.2"
5
5
  platform: ruby
6
6
  authors:
7
7
  - Conor Hunt
@@ -32,7 +32,7 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.9.7
34
34
  version:
35
- description: Rails generator to create admin views for models
35
+ description: Rails generator to create admin scaffolding with searching and sorting for models
36
36
  email: conor.hunt@gmail.com
37
37
  executables: []
38
38
 
@@ -47,7 +47,6 @@ files:
47
47
  - generators/admin_views/admin_views_generator.rb
48
48
  - generators/admin_views/templates/controllers/controller.rb
49
49
  - generators/admin_views/templates/views/_form.html.erb
50
- - generators/admin_views/templates/views/_model.html.erb
51
50
  - generators/admin_views/templates/views/edit.html.erb
52
51
  - generators/admin_views/templates/views/index.html.erb
53
52
  - generators/admin_views/templates/views/new.html.erb
@@ -79,6 +78,6 @@ rubyforge_project:
79
78
  rubygems_version: 1.3.5
80
79
  signing_key:
81
80
  specification_version: 3
82
- summary: Rails generator to create admin views for models
81
+ summary: Rails generator to create admin scaffolding with searching and sorting for models
83
82
  test_files: []
84
83
 
@@ -1,9 +0,0 @@
1
- <tr id="<%= model_name %>_<%%= <%= model_name %>.id %>">
2
- <% for col in columns -%>
3
- <td><%%= h <%= model_name %>.<%= col.name %> %></td>
4
- <% end -%>
5
- <td>
6
- <%%= link_to "Edit", edit_admin_<%= model_name %>_path(<%= model_name %>) %>
7
- <%%= link_to "Delete", admin_<%= model_name %>_path(<%= model_name %>), :method => :delete, :confirm => "Are you sure?" %>
8
- </td>
9
- </tr>