active_filter 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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
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.md ADDED
@@ -0,0 +1,46 @@
1
+ # ActiveFilter
2
+
3
+ ActiveFilter is a Rails engine for allowing users to filter scope dynamically.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'active_filter'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install active_filter
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ class BlogFilter < ActiveFilter::Base
23
+ model Blog
24
+ fields :title, :description
25
+ end
26
+ ```
27
+
28
+ And then in your controller you could do:
29
+
30
+ ```ruby
31
+ class BlogsController < ApplicationController
32
+ def index
33
+ @filter = BlogFilter.new(params)
34
+ @blogs = @filter.to_scope
35
+ end
36
+ end
37
+ ```
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
46
+
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ActiveFilter'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,89 @@
1
+ # coding: utf-8
2
+ require "active_filter/rule"
3
+
4
+ module ActiveFilter
5
+ class Base
6
+ def initialize(params={})
7
+ @params = params
8
+ end
9
+
10
+ private
11
+ def self._rules
12
+ # Class クラスのインスタンスである ActiveFilter::Base オブジェクトの
13
+ # インスタンス変数にルールを格納
14
+ @rules ||= []
15
+ end
16
+
17
+ def self._model
18
+ # Class クラスのインスタンスである ActiveFilter::Base オブジェクトの
19
+ # インスタンス変数にモデルの型を格納
20
+ @model
21
+ end
22
+
23
+ protected
24
+ def self.model(klass)
25
+ @model = klass
26
+ end
27
+
28
+ def self.rule(pattern, lmbda=nil, &block)
29
+ if lmbda.nil?
30
+ if block_given?
31
+ callback = block
32
+ else
33
+ # ラムダ式またはブロックのどちらか必須
34
+ raise ArgumentError, "lambda or block are required!"
35
+ end
36
+ else
37
+ callback = lmbda
38
+ end
39
+
40
+ _rules << ActiveFilter::Rule.new(pattern, &callback)
41
+ end
42
+
43
+ def self.field(name)
44
+ self.rule "^#{name}$" do |scope, value|
45
+ scope.where(name => value)
46
+ end
47
+ end
48
+
49
+ def self.fields(*names)
50
+ names.each do |name|
51
+ self.field(name)
52
+ end
53
+ end
54
+
55
+ public
56
+ def model
57
+ self.class._model
58
+ end
59
+
60
+ def rules
61
+ self.class._rules
62
+ end
63
+
64
+ def each(&block)
65
+ scope = self.to_scope
66
+ unless block_given?
67
+ return scope.each
68
+ end
69
+
70
+ scope.each do |obj|
71
+ block.call(obj)
72
+ end
73
+ self
74
+ end
75
+
76
+ def to_scope
77
+ scope = self.model.scoped
78
+ @params.each do |key, value|
79
+ # 最初にマッチしたルールを適用する
80
+ rule = self.rules.find { |r| r.match?(key) }
81
+ if rule
82
+ scope = rule.filter(scope, value)
83
+ end
84
+ end
85
+ scope
86
+ end
87
+ end
88
+ end
89
+
@@ -0,0 +1,5 @@
1
+ #coding: utf-8
2
+ module ActiveFilter #:nodoc:
3
+ class Engine < ::Rails::Engine #:nodoc:
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ module ActiveFilter
3
+ class Railtie < ::Rails::Railtie #:nodoc:
4
+ initializer 'active_filter' do |app|
5
+ end
6
+ end
7
+ end
8
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ module ActiveFilter
4
+ class Rule
5
+ attr_reader :pattern
6
+
7
+ def initialize(pattern, &block)
8
+ @pattern = pattern
9
+ @block = block
10
+ end
11
+
12
+ def match?(value)
13
+ @regexp ||= Regexp.new(@pattern)
14
+ matched = (@regexp =~ value.to_s)
15
+ !matched.nil?
16
+ end
17
+
18
+ def filter(scope, value)
19
+ @block.call(scope, value)
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,3 @@
1
+ module ActiveFilter
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ require "active_filter/version"
3
+ require "active_filter/rule"
4
+ require "active_filter/base"
5
+
6
+ module ActiveFilter
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,5 @@
1
+ class <%= @model_name %>Filter < ActiveFilter::Base
2
+ model <%= @model_name %>
3
+ fields <%= @fields.map { |f| ':' + f }.join(', ') %>
4
+ end
5
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :active_filter do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,110 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_filter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tnakamura
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 3.2.9
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: 3.2.9
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
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
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ActiveFilter is a Rails engine for allowing users to filter scope dynamically.
63
+ email:
64
+ - griefworker@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/active_filter/base.rb
70
+ - lib/active_filter/engine.rb
71
+ - lib/active_filter/railtie.rb
72
+ - lib/active_filter/rule.rb
73
+ - lib/active_filter/version.rb
74
+ - lib/active_filter.rb
75
+ - lib/generators/active_filter/templates/model_filter.rb.erb
76
+ - lib/tasks/active_filter_tasks.rake
77
+ - MIT-LICENSE
78
+ - Rakefile
79
+ - README.md
80
+ homepage: https://github.com/tnakamura/active_filter
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ segments:
93
+ - 0
94
+ hash: -333521642641220170
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ segments:
102
+ - 0
103
+ hash: -333521642641220170
104
+ requirements: []
105
+ rubyforge_project:
106
+ rubygems_version: 1.8.24
107
+ signing_key:
108
+ specification_version: 3
109
+ summary: A rails engine for filtering scope based on user selections.
110
+ test_files: []