filter_form 0.0.2

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0687dda492a9e82d4e3b5555586410a5679a52e7
4
+ data.tar.gz: 167fb3b2e8dc2cd001b2dd8c618088632cb12263
5
+ SHA512:
6
+ metadata.gz: 78954032e35a6c3e71dbdfb2e5e678a90a41a6d163e658d0f76240c775f7ec517dd0c26af01927135ad888548b9fac813f1a1d337da5540a773990cecbadb755
7
+ data.tar.gz: c6ed716a620c7465a14c1318c1dd130e37dad616588f276c58f9dc4ce49356f6ff32d186cbfc9bd90b2d5371f8d67752384cc622a161c53b91d319e5b393a2bf
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in filter_form.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Evgeny Li
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,76 @@
1
+ # FilterForm
2
+
3
+ Build filter forms easily by using `ransack` and `simple_form`.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'filter_form'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install filter_form
20
+
21
+ ## Usage
22
+
23
+ In your contoller:
24
+
25
+ ```ruby
26
+ def index
27
+ @q = Person.search(params[:q])
28
+ @people = @q.result
29
+ end
30
+ ```
31
+
32
+ In your view file:
33
+
34
+ ```erb
35
+ <%= filter_form_for @q do |f| %>
36
+ <%= f.filter_input :name %>
37
+ <%= f.filter_input :age %>
38
+ <%= f.filter_input :city_id %>
39
+ <%= f.filter_input :birthday %>
40
+ <%= f.button :submit %>
41
+ <% end %>
42
+ ```
43
+
44
+ For `string` attribute (like name) it will automatically create a text input with predicate `cont` (contains).
45
+
46
+ For `integer` type (age) it will set predicate `eq`.
47
+
48
+ For association's `foreign key` (city_id) it will automatically build a select tag.
49
+
50
+ For `date` and `datetime` (birthday) it will automatically add jQuery [datepicker](http://jqueryui.com/datepicker/) and set predicate `eq`.
51
+
52
+ If you want to use datepicker add to your application.js file:
53
+
54
+ ```js
55
+ //= require jquery
56
+ //= require jquery.ui.datepicker
57
+ //= require filter_form
58
+ ```
59
+
60
+ And application.css:
61
+
62
+ ```css
63
+ *= require jquery.ui.datepicker
64
+ ```
65
+
66
+ For more information about predicates visit [ransack](https://github.com/ernie/ransack).
67
+
68
+ If you want to customize your form visit [simple_form](https://github.com/plataformatec/simple_form).
69
+
70
+ ## Contributing
71
+
72
+ 1. Fork it
73
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
74
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
75
+ 4. Push to the branch (`git push origin my-new-feature`)
76
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'filter_form/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'filter_form'
7
+ spec.version = FilterForm::VERSION
8
+ spec.authors = ['Evgeny Li']
9
+ spec.email = ['exaspark@gmail.com']
10
+ spec.description = %q{Build filter forms easily}
11
+ spec.summary = %q{Build your filter forms automatically}
12
+ spec.homepage = 'https://github.com/exAspArk/filter_form'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ['lib']
19
+
20
+ spec.add_dependency 'jquery-rails'
21
+ spec.add_dependency 'simple_form'
22
+ spec.add_dependency 'ransack'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 1.3'
25
+ spec.add_development_dependency 'rake'
26
+ end
@@ -0,0 +1,3 @@
1
+ $ ->
2
+ $('.filter_form_date').datepicker
3
+ dateFormat: 'yy-mm-dd'
@@ -0,0 +1,15 @@
1
+ module FilterForm
2
+ module FormHelper
3
+ def filter_form_for(record, options = {}, &block)
4
+ options.reverse_merge!({
5
+ url: url_for,
6
+ method: :get,
7
+ html: { class: 'filter_form', novalidate: true }
8
+ })
9
+
10
+ simple_form_for(record, options, &block)
11
+ end
12
+ end
13
+ end
14
+
15
+ ActionView::Base.send :include, FilterForm::FormHelper
@@ -0,0 +1,113 @@
1
+ module FilterForm
2
+ class Input
3
+ DATE_CLASS = 'filter_form_date'
4
+
5
+ include ActiveModel::Model
6
+
7
+ attr_accessor :attribute_name, :object
8
+
9
+ def options
10
+ result = default_options
11
+
12
+ case type
13
+ when :select
14
+ result.merge!(select_options)
15
+ else
16
+ result[:input_html].merge!(value: input_value)
17
+ end
18
+
19
+ case attribute_type
20
+ when :datetime, :date
21
+ result[:input_html].merge!(class: DATE_CLASS)
22
+ end
23
+
24
+ result
25
+ end
26
+
27
+ private
28
+
29
+ def default_options
30
+ {
31
+ as: type,
32
+ required: false,
33
+ input_html: { name: input_name }
34
+ }
35
+ end
36
+
37
+ # Select options ------------------------------------------------------------
38
+
39
+ def select_options
40
+ {
41
+ collection: collection,
42
+ include_blank: true,
43
+ selected: selected
44
+ }
45
+ end
46
+
47
+ def collection
48
+ if reference_attribute?
49
+ attribute_name.to_s.gsub('_id', '').camelize.constantize.all
50
+ else
51
+ object.klass.pluck(attribute_name).uniq
52
+ end
53
+ end
54
+
55
+ def selected
56
+ if object_condition.present?
57
+ input_value
58
+ else
59
+ false
60
+ end
61
+ end
62
+
63
+ # ---------------------------------------------------------------------------
64
+
65
+ def input_name
66
+ "q[#{ input_attribute_name }]"
67
+ end
68
+
69
+ def type
70
+ return :select if reference_attribute?
71
+
72
+ case attribute_type
73
+ when :string, :integer, :datetime, :date
74
+ :string
75
+ when :float
76
+ :float
77
+ when :boolean
78
+ :radio_buttons
79
+ else
80
+ :select
81
+ end
82
+ end
83
+
84
+ def input_value
85
+ object_condition.values.first.value if object_condition
86
+ end
87
+
88
+ def reference_attribute?
89
+ attribute_name.to_s.end_with?('_id')
90
+ end
91
+
92
+ def object_condition
93
+ object.base.conditions.select { |c| c.a.first.name == attribute_name.to_s }.first
94
+ end
95
+
96
+ def input_attribute_name
97
+ "#{ attribute_name }_#{ predicate_name }"
98
+ end
99
+
100
+ def attribute_type
101
+ object.klass.columns_hash[attribute_name.to_s].type
102
+ end
103
+
104
+ def predicate_name
105
+ case attribute_type
106
+ when :string
107
+ 'cont'
108
+ else
109
+ 'eq'
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,12 @@
1
+ require 'filter_form/input'
2
+
3
+ module SimpleForm
4
+ class FormBuilder < ActionView::Helpers::FormBuilder
5
+ def filter_input(attribute_name, options = {}, &block)
6
+ filter_input = FilterForm::Input.new(attribute_name: attribute_name, object: object)
7
+ options.reverse_merge!(filter_input.options)
8
+
9
+ input(attribute_name, options, &block)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module FilterForm
2
+ VERSION = '0.0.2'
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'filter_form/version'
2
+ require 'filter_form/form_helper'
3
+ require 'filter_form/simple_form/form_builder'
4
+
5
+ module FilterForm
6
+ class Engine < ::Rails::Engine
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,125 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: filter_form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Evgeny Li
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jquery-rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: simple_form
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ransack
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Build filter forms easily
84
+ email:
85
+ - exaspark@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.md
93
+ - Rakefile
94
+ - filter_form.gemspec
95
+ - lib/assets/javascripts/filter_form.js.coffee
96
+ - lib/filter_form.rb
97
+ - lib/filter_form/form_helper.rb
98
+ - lib/filter_form/input.rb
99
+ - lib/filter_form/simple_form/form_builder.rb
100
+ - lib/filter_form/version.rb
101
+ homepage: https://github.com/exAspArk/filter_form
102
+ licenses:
103
+ - MIT
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.0.3
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Build your filter forms automatically
125
+ test_files: []