mongoid_sortable 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rbenv-gemsets ADDED
@@ -0,0 +1 @@
1
+ mongoid_sortable_gem
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_sortable.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem 'rake'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Egor Lynko
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # MongoidSortable
2
+ [![Build Status](https://travis-ci.org/flexoid/mongoid_sortable.png?branch=master)](https://travis-ci.org/flexoid/mongoid_sortable)
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ gem 'mongoid_sortable'
9
+
10
+ And then execute:
11
+
12
+ $ bundle
13
+
14
+ Or install it yourself as:
15
+
16
+ $ gem install mongoid_sortable
17
+
18
+ ## Usage
19
+
20
+ #### Model code:
21
+ ```ruby
22
+ class Book
23
+ include Mongoid::Document
24
+ include MongoidSortable::Sorting
25
+
26
+ sortable_by :title, :created_at
27
+ end
28
+ ```
29
+
30
+ #### Controller code:
31
+ ```ruby
32
+ @books = Book.all
33
+ ```
34
+
35
+ #### View code:
36
+ ```ruby
37
+ # Generates ?order_by=created_at_asc or ?order_by=created_at_desc
38
+ # depending on current @book criteria order
39
+ sorting_link(@book, :created_at, 'Book Timestamp')
40
+
41
+ # Customize link
42
+ sorting_link(@book, :created_at, 'Book Timestamp') do |name, path, current_order, is_active|
43
+ # Whatever you want to generate based on block parameters
44
+ end
45
+ ```
46
+
47
+ By default, sorting link has html classes *sorting_link asc/desc*
48
+ and *active* if sorting by that field is enabled now.
49
+
50
+ * * *
51
+
52
+ #### There is ability to customize queries generation.
53
+ ```ruby
54
+ sortable_by :domestic_gross, :foreign_gross do |gross_type|
55
+ "movie_details.grosses.#{gross_type}"
56
+ end
57
+ ```
58
+ Using this with `domestic_gross_desc`, for example,
59
+ generates desceting sorting by `movie_details.grosses.domestic_gross_desc` field.
60
+ It can be used to get rid of long url parameters which reflects your internal document structure.
61
+
62
+ Also you can set block that will be used to generate sorting field
63
+ for all parameters that don't match to explicit `sortable_by`
64
+ ```ruby
65
+ default_sorting do |param|
66
+ "movie_details.#{param}"
67
+ end
68
+ ```
69
+ So, for example, `sorting(order_by: ticket_cost_asc)` will generate query
70
+ with ascenting `movie_details.ticket_cost` sorting.
71
+
72
+ Of course, you can add more complicated logic inside of `sortable_by` and `default_sorting` block,
73
+ all that you need is to return string with field name.
74
+
75
+ ## Contributing
76
+
77
+ 1. Fork it
78
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
79
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
80
+ 4. Push to the branch (`git push origin my-new-feature`)
81
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,8 @@
1
+ require "mongoid_sortable/version"
2
+
3
+ require 'mongoid_sortable/sorting'
4
+ require 'mongoid_sortable/settings'
5
+
6
+ if defined?(Rails)
7
+ require 'mongoid_sortable/railtie'
8
+ end
@@ -0,0 +1,26 @@
1
+ module MongoidSortable
2
+ module Helpers
3
+ module SortingHelper
4
+ def sorting_link(sorting_criteria, field, name = nil, &block)
5
+ path_method = :"#{sorting_criteria.model_name.pluralize.underscore}_path"
6
+ current_order_value = sorting_criteria.options.sort.try(:[], field.to_s)
7
+ current_order = (current_order_value == 1) ? :asc : :desc
8
+
9
+ html_options = {class: "sorting-link #{current_order}"}
10
+ html_options[:class] += ' ' + 'active' if current_order_value
11
+
12
+ new_order = (current_order == :asc || !current_order_value) ? :desc : :asc
13
+
14
+ path = Rails.application.routes.url_helpers.
15
+ public_send(path_method, order_by: "#{field}_#{new_order}")
16
+ name ||= sorting_criteria.klass.human_attribute_name(field)
17
+
18
+ if block_given?
19
+ capture(name, path, current_order.to_s, !!current_order_value, &block)
20
+ else
21
+ link_to name, path, html_options
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ require 'mongoid_sortable/helpers/sorting_helper'
2
+
3
+ module MongoidSortable
4
+ class Railtie < Rails::Railtie
5
+ initializer "mongoid_sortable.helpers" do
6
+ ActionView::Base.send :include, Helpers::SortingHelper
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,23 @@
1
+ module MongoidSortable
2
+ class Settings
3
+ attr_accessor :fields
4
+ attr_accessor :sort_generation_blocks
5
+
6
+ def initialize
7
+ self.fields = []
8
+ self.sort_generation_blocks = {}
9
+ end
10
+
11
+ def add_fields(fields, &block)
12
+ self.fields.concat(fields).uniq!
13
+ add_block_to_fields(fields, &block) if block_given?
14
+ end
15
+
16
+ private
17
+ def add_block_to_fields(fields, &block)
18
+ fields.each do |field|
19
+ self.sort_generation_blocks.merge!(field => block)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,49 @@
1
+ module MongoidSortable
2
+ module Sorting
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ cattr_accessor :sortable_settings do
7
+ Settings.new
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def sortable_by(*fields, &block)
13
+ self.sortable_settings.add_fields(fields.flatten.map(&:to_sym), &block)
14
+ end
15
+
16
+ def default_sorting(&block)
17
+ sortable_by(:__default__, &block)
18
+ end
19
+
20
+ def sorting(params)
21
+ new_criteria = criteria
22
+ if params && params[:order_by]
23
+ prepare_sort_params(params).each do |field, order|
24
+ new_criteria = new_criteria.order_by(field => order)
25
+ end
26
+ end
27
+ new_criteria
28
+ end
29
+
30
+ private
31
+ def prepare_sort_params(params)
32
+ fields = self.sortable_settings.fields
33
+ sort_blocks = self.sortable_settings.sort_generation_blocks
34
+
35
+ [params[:order_by].match(/(.*)_(asc|desc)/)[1..2].map(&:to_sym)].map do |field, order|
36
+ if field.in? fields
37
+ if field.in? sort_blocks
38
+ [sort_blocks[field][field], order]
39
+ else
40
+ [field, order]
41
+ end
42
+ elsif fields.include? :__default__
43
+ [sort_blocks[:__default__][field], order]
44
+ end
45
+ end.compact
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module MongoidSortable
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid_sortable/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "mongoid_sortable"
8
+ gem.version = MongoidSortable::VERSION
9
+ gem.authors = ["Egor Lynko"]
10
+ gem.email = ["flexoid@gmail.com"]
11
+ gem.description = %q{Provides methods to sort mongoid collections.}
12
+ gem.summary = %q{Helper methods to sort mongoid collections on views}
13
+ gem.homepage = ""
14
+
15
+ gem.add_dependency('mongoid')
16
+ gem.add_development_dependency('rspec')
17
+
18
+ gem.files = `git ls-files`.split($/)
19
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
20
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
21
+ gem.require_paths = ["lib"]
22
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe MongoidSortable::Settings do
4
+ let(:settings) { MongoidSortable::Settings.new }
5
+
6
+ describe "fields accessor" do
7
+ it "exists" do
8
+ expect(settings).to be_respond_to(:fields)
9
+ expect(settings).to be_respond_to(:fields=)
10
+ end
11
+
12
+ it "has correct defauld value" do
13
+ expect(settings.fields).to eq([])
14
+ end
15
+ end
16
+
17
+ describe "#add_fields" do
18
+ it "adds new usual fields" do
19
+ settings.add_fields([:year, :genre])
20
+ expect(settings.fields).to eq([:year, :genre])
21
+
22
+ settings.add_fields([:director])
23
+ expect(settings.fields).to eq([:year, :genre, :director])
24
+ end
25
+
26
+ it "adds new fields with block" do
27
+ settings.add_fields([:premiere_date]) do |field|
28
+ "information.#{field}"
29
+ end
30
+ expect(settings.fields).to eq([:premiere_date])
31
+ expect(settings.sort_generation_blocks[:premiere_date].call(:premiere_date)).
32
+ to eq('information.premiere_date')
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ describe MongoidSortable::Sorting do
4
+ let(:movie_klass) do
5
+ Class.new do
6
+ include Mongoid::Document
7
+ include MongoidSortable::Sorting
8
+ end
9
+ end
10
+ let(:movie) { movie_klass.new }
11
+
12
+ describe "settings accessor" do
13
+ it "exist in class" do
14
+ expect(movie_klass.respond_to?(:sortable_settings)).to be_true
15
+ expect(movie_klass.respond_to?(:sortable_settings=)).to be_true
16
+ end
17
+
18
+ it "exist in class" do
19
+ expect(movie.respond_to?(:sortable_settings)).to be_true
20
+ expect(movie.respond_to?(:sortable_settings=)).to be_true
21
+ end
22
+
23
+ it "returns correct default value" do
24
+ expect(movie_klass.sortable_settings).to be_is_a(MongoidSortable::Settings)
25
+ end
26
+ end
27
+
28
+ describe "#sortable_by" do
29
+ before(:each) do
30
+ movie_klass.instance_eval do
31
+ sortable_by :director, :rating
32
+ end
33
+ end
34
+ let(:settings) { movie_klass.sortable_settings }
35
+
36
+ it "adds fields available for sorting" do
37
+ MongoidSortable::Settings.any_instance.should_receive(:add_fields).with([:year])
38
+ movie_klass.instance_eval do
39
+ sortable_by :year
40
+ end
41
+ end
42
+
43
+ it "adds fields with block for custom order generation" do
44
+ MongoidSortable::Settings.any_instance.should_receive(:add_fields).with([:domestic_gross, :foreign_gross])
45
+ movie_klass.instance_eval do
46
+ sortable_by :domestic_gross, :foreign_gross do |gross|
47
+ "grosses.#{gross}"
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ describe "#default_sorting" do
54
+ it "adds special :__default__ field to available for ordering list" do
55
+ MongoidSortable::Settings.any_instance.should_receive(:add_fields).with([:__default__])
56
+ movie_klass.instance_eval do
57
+ default_sorting do |param|
58
+ "additional_params.#{param}"
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ describe "#sorting" do
65
+ it "forms ordered query for usual field" do
66
+ movie_klass.instance_eval do
67
+ sortable_by :year
68
+ end
69
+ params = {order_by: 'year_asc'}
70
+ expect(movie_klass.sorting(params).options.sort).to eq('year' => 1)
71
+ end
72
+
73
+ it "forms ordered query for nested field" do
74
+ movie_klass.instance_eval do
75
+ sortable_by 'grosses.foreign_gross'
76
+ end
77
+ params = {order_by: 'grosses.foreign_gross_desc'}
78
+ expect(movie_klass.sorting(params).options.sort).to eq('grosses.foreign_gross' => -1)
79
+ end
80
+
81
+ it "forms ordered query for field with block" do
82
+ movie_klass.instance_eval do
83
+ sortable_by :domestic_gross, :foreign_gross do |gross|
84
+ "grosses.#{gross}"
85
+ end
86
+ end
87
+ params = {order_by: 'domestic_gross_desc'}
88
+ expect(movie_klass.sorting(params).options.sort).to eq('grosses.domestic_gross' => -1)
89
+ end
90
+
91
+ it "forms ordered query with default sorting block" do
92
+ movie_klass.instance_eval do
93
+ default_sorting do |param|
94
+ "additional_params.#{param}"
95
+ end
96
+ end
97
+ params = {order_by: 'ticket_cost_asc'}
98
+ expect(movie_klass.sorting(params).options.sort).to eq('additional_params.ticket_cost' => 1)
99
+ end
100
+
101
+ it "forms query without sorting options" do
102
+ params = {}
103
+ expect(movie_klass.sorting(params).options.sort).to be_nil
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
3
+
4
+ require 'active_support/all'
5
+ require 'mongoid'
6
+ require 'mongoid_sortable'
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_sortable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Egor Lynko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-18 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Provides methods to sort mongoid collections.
47
+ email:
48
+ - flexoid@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - .rbenv-gemsets
55
+ - Gemfile
56
+ - LICENSE.txt
57
+ - README.md
58
+ - Rakefile
59
+ - lib/mongoid_sortable.rb
60
+ - lib/mongoid_sortable/helpers/sorting_helper.rb
61
+ - lib/mongoid_sortable/railtie.rb
62
+ - lib/mongoid_sortable/settings.rb
63
+ - lib/mongoid_sortable/sorting.rb
64
+ - lib/mongoid_sortable/version.rb
65
+ - mongoid_sortable.gemspec
66
+ - spec/mongoid_sortable/settings_spec.rb
67
+ - spec/mongoid_sortable/sorting_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: ''
70
+ licenses: []
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 1.8.23
90
+ signing_key:
91
+ specification_version: 3
92
+ summary: Helper methods to sort mongoid collections on views
93
+ test_files:
94
+ - spec/mongoid_sortable/settings_spec.rb
95
+ - spec/mongoid_sortable/sorting_spec.rb
96
+ - spec/spec_helper.rb