sortable_columns 0.0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+
7
+ spec/internal/db/*.sqlite3
8
+ spec/internal/log/*.log
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.0.1
2
+
3
+ * initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in sortable_columns.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Brian McNabb
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.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = sortable_columns
2
+
3
+ A clean and lightweight solution for making table columns sortable.
4
+
5
+ == Features
6
+
7
+ === Easy to use
8
+ Just bundle the gem, then your models are ready to be sorted by columns. No configuration required. Don't have to define anything in your models or helpers.
9
+
10
+ === Simple scope-based API
11
+ Everything is method chainable.
12
+
13
+ == Install
14
+
15
+ Put this line in your Gemfile:
16
+ gem 'sortable_columns'
17
+
18
+ Then bundle:
19
+ % bundle
20
+
21
+
22
+ == Usage
23
+
24
+ === Query Basics
25
+
26
+ * the +sort+ scope
27
+
28
+ To sort the 'updated_at' column descending from the most recently updated to the oldest (default +column+ is 'created_at' || default +direction+ is 'ascending)
29
+ User.sort('updated_at', 'descending')
30
+
31
+ === Controllers
32
+
33
+ * the sort parameter is in <tt>params[:sort]</tt>
34
+
35
+ Typically, your controller code will look like this:
36
+ @collection = Resource.sort(params[:sort])
37
+
38
+ === Views
39
+
40
+ * the same old helper method
41
+
42
+ Just call the +sortable+ helper:
43
+ <%= sortable column_name %>
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2011 Brian McNabb. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new(:spec) do |spec|
6
+ spec.pattern = FileList['spec/**/*_spec.rb']
7
+ end
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize!
7
+ run Combustion::Application
@@ -0,0 +1,13 @@
1
+ module SortableColumns
2
+ module ActionViewExtensions
3
+ module SortHelper
4
+
5
+ def sortable(column, title = nil)
6
+ title ||= column.titleize
7
+ css_class = column == params[:column] ? "current #{ params[:direction] }" : nil
8
+ direction = column == params[:column] && params[:direction] == "asc" ? "desc" : "asc"
9
+ link_to title, params.merge(column: column, direction: direction), {:class => css_class}
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,30 @@
1
+ module SortableColumns
2
+ class ColumnSorter
3
+ attr_reader :resource
4
+
5
+ def initialize(resource)
6
+ @resource = resource
7
+ end
8
+
9
+ def column(column=nil)
10
+ return SortableColumns.config.default_sort_column if column.nil?
11
+ sanitize_column(column) || SortableColumns.config.default_sort_column
12
+ end
13
+
14
+ def direction(direction=nil)
15
+ return SortableColumns.config.default_sort_direction if direction.nil?
16
+ sanitize_direction(direction) || SortableColumns.config.default_sort_direction
17
+ end
18
+
19
+
20
+ private
21
+
22
+ def sanitize_column(column)
23
+ return column if @resource.attribute_names.include?(column)
24
+ end
25
+
26
+ def sanitize_direction(direction)
27
+ return direction if %w[asc desc].include?(direction)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ require 'active_support/configurable'
2
+
3
+ module SortableColumns
4
+ # Configures global settings for SortableColumns
5
+ # SortableColumns.configure do |config|
6
+ # config.default_sort_column = 'created_at'
7
+ # end
8
+ def self.configure(&block)
9
+ yield @config ||= SortableColumns::Configuration.new
10
+ end
11
+
12
+ # Global settings for SortableColumns
13
+ def self.config
14
+ @config
15
+ end
16
+
17
+ class Configuration #:nodoc:
18
+ include ActiveSupport::Configurable
19
+ config_accessor :default_sort_column
20
+ config_accessor :default_sort_direction
21
+ end
22
+
23
+ configure do |config|
24
+ config.default_sort_column = 'created_at'
25
+ config.default_sort_direction = 'desc'
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module SortableColumns
2
+ VERSION = "0.0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'action_view'
2
+ require 'sortable_columns/config'
3
+ require 'sortable_columns/column_sorter'
4
+
5
+ module SortableColumns
6
+ ActiveSupport.on_load(:action_view) do
7
+ require 'sortable_columns/action_view_extensions/sort_helper'
8
+ include SortableColumns::ActionViewExtensions::SortHelper
9
+ end
10
+ end
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "sortable_columns/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "sortable_columns"
7
+ s.version = SortableColumns::VERSION
8
+ s.authors = ["Brian McNabb"]
9
+ s.email = ["brian.d.mcnabb@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Make your columns sortable... in a snap!}
12
+ s.description = %q{'sortable_columns' is a clean and lightweight solution for making table columns sortable.}
13
+
14
+ s.rubyforge_project = "sortable_columns"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency 'combustion', '~> 0.3.1'
22
+ s.add_development_dependency 'sqlite3'
23
+ s.add_development_dependency 'rspec'
24
+ s.add_development_dependency 'rspec-rails'
25
+ s.add_development_dependency 'capybara'
26
+ s.add_development_dependency 'factory_girl_rails'
27
+ end
data/spec/factories.rb ADDED
@@ -0,0 +1,6 @@
1
+ Factory.define :user do |f|
2
+ f.sequence(:name) { |n| "John #{('A'..'Z').to_a[(n/2)]} Doe" }
3
+ f.bio "I'm a fake user created with Factory_Girl"
4
+ f.sequence(:age) { |n| "#{n}" }
5
+ f.sequence(:years_till_retirement) { |n| "#{65 - n}" }
6
+ end
@@ -0,0 +1,2 @@
1
+ class User < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ #
3
+ end
@@ -0,0 +1,9 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table(:users, :force => true) do |t|
3
+ t.string :name
4
+ t.text :bio
5
+ t.text :age
6
+ t.text :years_till_retirement
7
+ t.timestamps
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,34 @@
1
+ require File.expand_path('../spec_helper', File.dirname(__FILE__))
2
+
3
+ shared_examples_for 'default' do
4
+ its('last.name') { should == 'John A Doe' }
5
+ end
6
+
7
+ describe SortableColumns::ActiveRecordExtension do
8
+ before :all do
9
+ (1..50).each { Factory(:user) }
10
+ end
11
+
12
+ describe '#sort' do
13
+ context 'sort without any argument (defaults)' do
14
+ subject { User.sort }
15
+ it_should_behave_like 'default'
16
+ end
17
+
18
+ context 'sort with only column argument passed' do
19
+ subject { User.sort('years_till_retirement') }
20
+ its('first.name') { should == 'John A Doe' }
21
+ end
22
+
23
+ context 'sort with both arguments passed' do
24
+ subject { User.sort('years_till_retirement', 'asc') }
25
+ its('last.name') { should == 'John A Doe' }
26
+ end
27
+
28
+ context 'sort with invalid arguments passed' do
29
+ subject { User.sort('invalid_column', 'invalid_direction') }
30
+ it_should_behave_like 'default'
31
+ end
32
+ end
33
+
34
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ require 'capybara/rspec'
7
+
8
+ Combustion.initialize!
9
+
10
+ require 'rspec/rails'
11
+ require 'capybara/rails'
12
+ require File.join(File.dirname(__FILE__), 'factories')
13
+
14
+ RSpec.configure do |config|
15
+ config.use_transactional_fixtures = true
16
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sortable_columns
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian McNabb
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-10 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: combustion
16
+ requirement: &70132009918840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.3.1
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70132009918840
25
+ - !ruby/object:Gem::Dependency
26
+ name: sqlite3
27
+ requirement: &70132009904580 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70132009904580
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70132009903040 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70132009903040
47
+ - !ruby/object:Gem::Dependency
48
+ name: rspec-rails
49
+ requirement: &70132009901860 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70132009901860
58
+ - !ruby/object:Gem::Dependency
59
+ name: capybara
60
+ requirement: &70132009900780 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70132009900780
69
+ - !ruby/object:Gem::Dependency
70
+ name: factory_girl_rails
71
+ requirement: &70132009899800 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ! '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *70132009899800
80
+ description: ! '''sortable_columns'' is a clean and lightweight solution for making
81
+ table columns sortable.'
82
+ email:
83
+ - brian.d.mcnabb@gmail.com
84
+ executables: []
85
+ extensions: []
86
+ extra_rdoc_files: []
87
+ files:
88
+ - .gitignore
89
+ - .rspec
90
+ - CHANGELOG.md
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.rdoc
94
+ - Rakefile
95
+ - config.ru
96
+ - lib/sortable_columns.rb
97
+ - lib/sortable_columns/action_view_extensions/sort_helper.rb
98
+ - lib/sortable_columns/column_sorter.rb
99
+ - lib/sortable_columns/config.rb
100
+ - lib/sortable_columns/version.rb
101
+ - sortable_columns.gemspec
102
+ - spec/factories.rb
103
+ - spec/internal/app/models/user.rb
104
+ - spec/internal/config/database.yml
105
+ - spec/internal/config/routes.rb
106
+ - spec/internal/db/schema.rb
107
+ - spec/internal/log/.gitignore
108
+ - spec/internal/public/favicon.ico
109
+ - spec/model/scopes_spec.rb
110
+ - spec/spec_helper.rb
111
+ homepage: ''
112
+ licenses: []
113
+ post_install_message:
114
+ rdoc_options: []
115
+ require_paths:
116
+ - lib
117
+ required_ruby_version: !ruby/object:Gem::Requirement
118
+ none: false
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ none: false
125
+ requirements:
126
+ - - ! '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project: sortable_columns
131
+ rubygems_version: 1.8.10
132
+ signing_key:
133
+ specification_version: 3
134
+ summary: Make your columns sortable... in a snap!
135
+ test_files:
136
+ - spec/factories.rb
137
+ - spec/internal/app/models/user.rb
138
+ - spec/internal/config/database.yml
139
+ - spec/internal/config/routes.rb
140
+ - spec/internal/db/schema.rb
141
+ - spec/internal/log/.gitignore
142
+ - spec/internal/public/favicon.ico
143
+ - spec/model/scopes_spec.rb
144
+ - spec/spec_helper.rb