list_controls 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Laurynas Butkus
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,62 @@
1
+ list_controls
2
+ =============
3
+
4
+ Simple list filtering & sorting for Rails controller.
5
+
6
+ Plays nicely with SearchLogic.
7
+
8
+
9
+ Example
10
+ =======
11
+
12
+ Controller:
13
+
14
+ class ProductsController < ApplicationController
15
+
16
+ can_filter_and_sort
17
+
18
+ def default_filters
19
+ { 'state' => 'new' }
20
+ end
21
+
22
+ def index
23
+ @products = Product.all :conditions => { :state => @filters.state }
24
+ end
25
+
26
+ end
27
+
28
+ # Note:
29
+ #
30
+ # Sort param stored in @filters.order, example 'ascend_by_title'
31
+ # Easy use with SearchLogic.
32
+
33
+
34
+ View:
35
+
36
+ <div id="filters">
37
+ <%= form_for :filters, @filters, :html => { :method => :get } do |f| %>
38
+
39
+ <%= f.label :state %>
40
+ <%= f.select :state, states_for_filter %>
41
+
42
+ <% end %>
43
+ </div>
44
+
45
+ <table>
46
+ <tr>
47
+ <th><%= sort @filters, :by => :id %></th>
48
+ <th><%= sort @filters, :by => :title, :as => t(:product_title) %></th>
49
+ <th><%= sort @filters, :by => :state, :as => t(:state) %></th>
50
+ </tr>
51
+
52
+ <% for product in @products %>
53
+ <tr>
54
+ <td><%= product.id %></td>
55
+ <td><%= product.title %></td>
56
+ <td><%= product.state %></td>
57
+ </tr>
58
+ <% end %>
59
+ </table>
60
+
61
+
62
+ Copyright (c) 2009 Laurynas Butkus, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the filter_and_sort plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the filter_and_sort plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'FilterAndSort'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,42 @@
1
+ module ActionController
2
+ module ListControls
3
+
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
7
+
8
+ module ClassMethods
9
+ def can_filter_and_sort(options = {})
10
+ include ActionController::ListControls::InstanceMethods
11
+
12
+ attr_reader :filters
13
+
14
+ before_filter :set_filters
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+
20
+ protected
21
+
22
+ def default_filters
23
+ {}
24
+ end
25
+
26
+ def set_filters
27
+ session[:list_controls]||= {}
28
+
29
+ session_store = session[:list_controls][self.class.to_s]||= {}
30
+ session_store[:filters] ||= default_filters
31
+ session_store[:filters].merge!(params[:filters] || {})
32
+
33
+ @filters = session_store[:filters]
34
+
35
+ session[:list_controls][self.class.to_s] = session_store
36
+ end
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+ ::ActionController::Base.send(:include, ActionController::ListControls)
@@ -0,0 +1,39 @@
1
+ module ListControls
2
+ module RailsHelpers
3
+ def sort(filters, options = {}, html_options = {})
4
+ options[:params_scope] ||= :filters
5
+ options[:ascend_scope] ||= "ascend_by_#{options[:by]}"
6
+ options[:descend_scope] ||= "descend_by_#{options[:by]}"
7
+ options[:sort_class] ||= 'sort'
8
+
9
+ current_order = filters['order']
10
+
11
+ ascending = current_order == options[:ascend_scope]
12
+ new_scope = ascending ? options[:descend_scope] : options[:ascend_scope]
13
+ selected = [options[:ascend_scope], options[:descend_scope]].include?(current_order)
14
+
15
+ css_classes = html_options[:class] ? html_options[:class].split(" ") : []
16
+
17
+ if selected
18
+ if ascending
19
+ css_classes << options[:sort_class] + "-up"
20
+ else
21
+ css_classes << options[:sort_class] + "-down"
22
+ end
23
+ else
24
+ css_classes << options[:sort_class]
25
+ end
26
+
27
+ html_options[:class] = css_classes.join(" ")
28
+
29
+ url_options = {
30
+ options[:params_scope] => { :order => new_scope }
31
+ }.deep_merge(options[:params] || {})
32
+
33
+ link_to options[:as] || options[:by], url_for(url_options), html_options
34
+ end
35
+ end
36
+ end
37
+
38
+ ::ActionController::Base.helper(ListControls::RailsHelpers)
39
+
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: list_controls
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Laurynas Butkus
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-01-08 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: ruby-debug
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: Simple list filtering & sorting for Rails controller.
36
+ email: laurynas.butkus@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ files:
44
+ - lib/action_controller.rb
45
+ - lib/rails_helpers.rb
46
+ - README
47
+ - MIT-LICENSE
48
+ - Rakefile
49
+ has_rdoc: true
50
+ homepage: http://github.com/laurynas/list_controls
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ hash: 3
64
+ segments:
65
+ - 0
66
+ version: "0"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project: "[none]"
79
+ rubygems_version: 1.3.7
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Simple list filtering & sorting for Rails controller
83
+ test_files: []
84
+