bolter 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 388ad37ede862d5892971b1ba805ee03bf93ddb6
4
+ data.tar.gz: 33e039682406dfb86420287c258e553d4984330d
5
+ SHA512:
6
+ metadata.gz: d1a2ddf0dd73f69f9faeb1d47caf7c9cd35a7b7623bfac1c450b533509ae7bd3a5a4e859ad9f4c135b0b349dee328b5134ed71ef691f1f8887fc33498d0a1106
7
+ data.tar.gz: 2895c37fbef6c5379166e5f4494debeed3482ef96d76448f937d45df562cde2654353616b6e92afb3a6beef976157d3878126530d09c7a486719b1911179dc8e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in bolter.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Evgeniy Shurmin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # Bolter
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/bolter.svg)](http://badge.fury.io/rb/bolter)
4
+
5
+ This gem allow you create search form and sorting in you views
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bolter'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install bolter
22
+
23
+ ## Usage
24
+
25
+ Model:
26
+ ```ruby
27
+ class Book < ActiveRecord::Base
28
+ filter :with_author, -> (author) {
29
+ self.where("author like ?",author)
30
+ }
31
+ end
32
+ ```
33
+
34
+ View:
35
+ ```haml
36
+ .search
37
+ = form_tag books_path, method: :get, id: :search do
38
+ = fields_for :filters, OpenStruct.new(params[:filters]) do |f|
39
+ = f.label :with_name, EventTarget.human_attribute_name(:name)
40
+ = f.text_field :with_name, :class => 'form-control'
41
+ .index
42
+ %table
43
+ %tr
44
+ %th= sort_link :author, Book.human_attribute_name(:author)
45
+ - @books.each do |book|
46
+ %tr
47
+ %td= book.author
48
+ ```
49
+
50
+ Controller:
51
+ ```ruby
52
+ @books = Book.search(params[:filters]).sorting(params[:sorting])
53
+ ```
54
+
55
+
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it ( https://github.com/jpascal/bolter/fork )
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bolter"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/bolter.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'bolter/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'bolter'
8
+ spec.version = Bolter::VERSION
9
+ spec.authors = ['Evgeniy Shurmin']
10
+ spec.email = ['eshurmin@gmail.com']
11
+ spec.summary = %q{This gem include sorting and filtering methods}
12
+ spec.homepage = 'http://github.com/jpascal/bolter'
13
+ spec.license = 'MIT'
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
16
+ spec.require_paths = ['lib']
17
+
18
+ spec.add_development_dependency 'bundler', '~> 1.8'
19
+ spec.add_development_dependency 'rake', '~> 10.0'
20
+ spec.add_development_dependency 'rails'
21
+ spec.add_development_dependency 'mongoid'
22
+
23
+ spec.add_dependency 'activesupport'
24
+ end
@@ -0,0 +1,19 @@
1
+ module Bolter
2
+ module UrlHelpers
3
+ def sort_link(scope_name, text = nil)
4
+ sorting = params[:sorting] || "#{scope_name}:asc"
5
+ current_field, direction = sorting.split(':', 2).map(&:to_s)
6
+ direction = direction == 'asc' ? 'desc' : 'asc'
7
+ title = text || scope
8
+ title = title + "#{direction == 'asc' ? '&#9650;' : '&#9660;'}" if scope_name.to_s == current_field and params[:sorting].present?
9
+ link_to title.html_safe, url_for(params.merge({:sorting => "#{scope_name}:#{direction}"}))
10
+ end
11
+ end
12
+ module FormHelpers
13
+ def self.field_for_filters(name, options = {}, &block)
14
+ record_object = OpenStruct.new(params[name])
15
+ builder = instantiate_builder(name, record_object, options)
16
+ silence(builder, &block)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails/railtie'
2
+ require 'bolter/helpers'
3
+ require 'bolter/searchable'
4
+ require 'bolter/sortable'
5
+
6
+ module Bolter
7
+ class Railtie < Rails::Railtie
8
+ initializer 'bolter.initialization' do
9
+ ActiveSupport.on_load(:action_view) do
10
+ ActionView::Base.send :include, Bolter::FormHelpers
11
+ ActionView::Base.send :include, Bolter::UrlHelpers
12
+ end
13
+
14
+ ActiveSupport.on_load(:active_record) do
15
+ ActiveRecord::Base.send :include, Bolter::Searchable
16
+ ActiveRecord::Base.send :include, Bolter::Sortable
17
+ end
18
+
19
+ if defined? ::Mongoid::Document
20
+ ActiveSupport.on_load(:mongoid) do
21
+ Mongoid::Document.send :include, Bolter::Searchable
22
+ Mongoid::Document.send :include, Bolter::Sortable
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,40 @@
1
+ module Bolter
2
+ module Searchable
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ cattr_reader :filters
6
+ def self.filter(name, value, &block)
7
+ @@filters ||= []
8
+ @@filters << name.to_s
9
+ scope name.to_sym, value, &block
10
+ end
11
+ # options[:filters] - used filters, default for all
12
+ def self.search(params = {}, options = {})
13
+ # Prepare filters
14
+ filters = Array(options[:filters] || self.filters).map(&:to_s)
15
+
16
+ # Prepare default result
17
+ result = self.scoped
18
+
19
+ # Prepare params to search
20
+ search = (params || {}).clone.reject{|_,value| value.to_s.empty? }
21
+
22
+ # Apply filters
23
+ search.each do |name, value|
24
+ if self.scopes.include? name.to_sym
25
+ if filters.include? name.to_s
26
+ result = self.scopes[name.to_sym][:scope].arity == 0 ? result.send(name) : result.send(name, *value)
27
+ else
28
+ logger.warn "Skip filter #{self}.#{name}(#{value.inspect})"
29
+ end
30
+ else
31
+ logger.warn "Unknown filter #{self}.#{name}(#{value.inspect})"
32
+ end
33
+ end
34
+
35
+ # return scope with applied filters
36
+ return result
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ module Bolter
2
+ module Sortable
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ def self.sorting(sorting)
6
+ return self.scoped unless sorting
7
+ field, direction = sorting.split(':',2)
8
+ if self.attribute_names.include? field.to_s
9
+ if %W[asc desc].include? direction
10
+ self.scoped.order(Hash[field, direction])
11
+ end
12
+ else
13
+ self.scoped
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Bolter
2
+ VERSION = '0.1.0'
3
+ end
data/lib/bolter.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'bolter/version'
2
+
3
+ module Bolter
4
+
5
+ end
6
+
7
+ require 'bolter/railtie' if defined?(Rails)
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bolter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Evgeniy Shurmin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mongoid
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - eshurmin@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - CODE_OF_CONDUCT.md
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - bolter.gemspec
99
+ - lib/bolter.rb
100
+ - lib/bolter/helpers.rb
101
+ - lib/bolter/railtie.rb
102
+ - lib/bolter/searchable.rb
103
+ - lib/bolter/sortable.rb
104
+ - lib/bolter/version.rb
105
+ homepage: http://github.com/jpascal/bolter
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.5
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: This gem include sorting and filtering methods
129
+ test_files: []