ransack_ui 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,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/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Include rake in Gemfile so that `bundle exec rake` doesn't raise an error
4
+ gem 'rake', :group => :test
5
+
6
+ # Specify your gem's dependencies in ransack_ui.gemspec
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Nathan Broadbent
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,27 @@
1
+ # Ransack UI
2
+
3
+ Provides HTML templates and JavaScript to build a fully functional
4
+ advanced search form using Ransack.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'ransack_ui'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install ransack_ui
19
+
20
+
21
+ ## Contributing
22
+
23
+ 1. Fork it
24
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
25
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
26
+ 4. Push to the branch (`git push origin my-new-feature`)
27
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
Binary file
@@ -0,0 +1,38 @@
1
+ window.Ransack ?= {}
2
+
3
+ Ransack.predicates =
4
+ eq: 'not_eq'
5
+ cont: 'not_cont'
6
+ matches: 'does_not_match'
7
+ start: 'not_start'
8
+ end: 'not_end'
9
+ present: 'blank'
10
+ null: 'not_null'
11
+ lt: 'gteq'
12
+ gt: 'lteq'
13
+ in: 'not_in'
14
+ true: 'false'
15
+
16
+ # Setup supported predicates for each column type.
17
+ Ransack.type_predicates = {}
18
+ ((o, f) -> f.call o) Ransack.type_predicates, ->
19
+ @text = @string = ['eq', 'cont', 'matches', 'start', 'end', 'present', 'in']
20
+ @boolean = ['true']
21
+ @integer = @float = @decimal = ['eq', 'null', 'lt', 'gt', 'in']
22
+ @date = @datetime = @time = ['eq', 'null', 'lt', 'gt']
23
+
24
+ # Setup input field types for each predicate
25
+ Ransack.predicate_inputs = {}
26
+ ((o, f) -> f.call o) Ransack.predicate_inputs, ->
27
+ @cont = @matches = @start = @end = @in = 'string'
28
+ @present = @null = @true = false
29
+ @eq = @gt = @lt = (type) ->
30
+ switch type
31
+ when 'string','text' then 'string'
32
+ when 'integer','float','decimal' then 'numeric'
33
+ when 'date','datetime','time' then type
34
+ else false # Hide for unhandled types.
35
+
36
+ # Use a tags input for 'in' if Select2 is available
37
+ if Select2?
38
+ Ransack.predicate_inputs.in = 'tags'
@@ -0,0 +1,113 @@
1
+ (($) ->
2
+ $.widget 'ransack.search_form',
3
+ options: {}
4
+
5
+ _create: ->
6
+ el = this.element
7
+ el.on 'click', '.add_fields', $.proxy(this.add_fields, this)
8
+ el.on 'click', '.remove_fields', $.proxy(this.remove_fields, this)
9
+ el.on 'change', 'select.ransack_predicate', $.proxy(this.predicate_changed, this)
10
+ el.on 'change', 'select.ransack_attribute', $.proxy(this.attribute_changed, this)
11
+
12
+ # Set up Select2 on select lists in .filters
13
+ this.init_select2(this.element.find('.filters'))
14
+
15
+ # show spinner and disable the form when the search is underway
16
+ el.find("form input:submit").click $.proxy(this.form_submit, this)
17
+
18
+ # Fire change event for any existing selects.
19
+ el.find(".filters select").change()
20
+
21
+ # For basic search, remove placeholder text on focus, restore on blur
22
+ $('#query').focusin (e) ->
23
+ $(this).data('placeholder', $(this).attr('placeholder')).attr('placeholder', '')
24
+ $('#query').focusout (e) ->
25
+ $(this).attr('placeholder', $(this).data('placeholder'))
26
+
27
+ predicate_changed: (e) ->
28
+ target = $(e.currentTarget)
29
+ value_el = $('input#' + target.attr('id').slice(0, -1) + "v_0_value")
30
+ if target.val() in ["true", "false", "blank", "present", "null", "not_null"]
31
+ value_el.val("true")
32
+ value_el.hide()
33
+ else
34
+ unless value_el.is(":visible")
35
+ value_el.val("")
36
+ value_el.show()
37
+
38
+ attribute_changed: (e) ->
39
+ target = $(e.currentTarget)
40
+ predicate_select = this.element.find('select#' + target.attr('id').slice(0, -8) + "p")
41
+ previous_val = predicate_select.val()
42
+ type = target.find('option:selected').data('type')
43
+
44
+ # Build array of supported predicates
45
+ available = predicate_select.data['predicates']
46
+
47
+ predicates = Ransack.type_predicates[type] || []
48
+ predicates = $.map predicates, (p) -> [p, Ransack.predicates[p]]
49
+
50
+ # Remove all predicates, and add any supported predicates
51
+ predicate_select.find('option').each (i, o) -> $(o).remove()
52
+
53
+ $.each available, (i, p) ->
54
+ [val, label] = [p[0], p[1]]
55
+ if val in predicates
56
+ predicate_select.append $('<option value='+val+'>'+label+'</option>')
57
+
58
+ # Select first predicate if current selection is invalid
59
+ predicate_select.select2('val', previous_val)
60
+
61
+ return true
62
+
63
+ form_submit: (e) ->
64
+ $("#loading").show()
65
+ this.element.css({ opacity: 0.4 })
66
+ $('div.list').html('')
67
+ true
68
+
69
+ add_fields: (e) ->
70
+ target = $(e.currentTarget)
71
+ type = target.data("fieldType")
72
+ content = target.data("content")
73
+ new_id = new Date().getTime()
74
+ regexp = new RegExp('new_' + type, 'g')
75
+ container = target.closest('p')
76
+ container.before content.replace(regexp, new_id)
77
+ this.init_select2 container.prev()
78
+ # Fire change event on any new selects.
79
+ container.prev().find("select").change()
80
+ false
81
+
82
+ remove_fields: (e) ->
83
+ target = $(e.currentTarget)
84
+ container = target.closest('.fields')
85
+ if (container.siblings().length > 1)
86
+ container.remove()
87
+ else
88
+ container.parent().closest('.fields').remove()
89
+ false
90
+
91
+ init_select2: (container) ->
92
+ if Select2?
93
+ # Store current predicates in data attribute
94
+ predicate_select = container.find('select.ransack_predicate')
95
+ unless predicate_select.data['predicates']
96
+ predicates = []
97
+ predicate_select.find('option').each (i, o) ->
98
+ $o = $(o)
99
+ predicates.push [$o.val(), $o.text()]
100
+ predicate_select.data['predicates'] = predicates
101
+
102
+ container.find('select.ransack_predicate').select2
103
+ width: '130px'
104
+ formatNoMatches: (term) ->
105
+ "No predicates found"
106
+
107
+ container.find('select.ransack_attribute').select2
108
+ width: '220px'
109
+ placeholder: "Select a Field"
110
+ allowClear: true
111
+ formatSelection: (object, container) ->
112
+ $(object.element).parent().attr('label') + ': ' + object.text
113
+ ) jQuery
@@ -0,0 +1,31 @@
1
+ (($) ->
2
+ $ ->
3
+ # Search tabs
4
+ # -----------------------------------------------------
5
+ activate_search_form = (search_form) ->
6
+ # Hide all
7
+ $('#search .search_form').hide()
8
+ $('#search .tabs li a').removeClass('active')
9
+ # Show selected
10
+ $('#' + search_form).show()
11
+ $('a[data-search-form=' + search_form + ']').addClass('active')
12
+ # Run search for current query
13
+ switch search_form
14
+ when 'basic_search'
15
+ query_input = $('#basic_search input#query')
16
+ if !query_input.is('.defaultTextActive')
17
+ value = query_input.val()
18
+ else
19
+ value = ""
20
+ crm.search(value, window.controller)
21
+ $('#filters').enable() # Enable filters panel (if present)
22
+
23
+ when 'advanced_search'
24
+ $("#advanced_search form input:submit").click()
25
+ $('#filters').disable() # Disable filters panel (if present)
26
+
27
+ return
28
+ $("#search .tabs a").click ->
29
+ activate_search_form($(this).data('search-form'))
30
+
31
+ ) jQuery
@@ -0,0 +1,3 @@
1
+ //= require ransack/predicates
2
+ //= require ransack_ui_jquery/search_form
3
+ //= require ransack_ui_jquery/search_tabs
@@ -0,0 +1,12 @@
1
+ = search_form_for search, :url => url_for(:action => :index), :html => {:method => :get, :class => "advanced_search"}, :remote => true do |f|
2
+
3
+ = f.grouping_fields do |g|
4
+ = render 'ransack/grouping_fields', :f => g, :options => options
5
+
6
+ %p
7
+ = link_to_add_fields t(:advanced_search_add_group), f, :grouping, options
8
+
9
+ %p
10
+ = hidden_field_tag :distinct, '1'
11
+ = hidden_field_tag :page, '1'
12
+ = f.submit t(:advanced_search_submit)
@@ -0,0 +1,2 @@
1
+ %div{ :style => "margin: 0px 0px 6px 0px" }
2
+ = text_field_tag('query', @current_query, :size => 32, :placeholder => "Search #{controller_name}")
@@ -0,0 +1,13 @@
1
+ .fields.condition{ "data-object-name" => f.object_name }
2
+ %p
3
+ = link_to_remove_fields t(:advanced_search_remove_condition), f
4
+
5
+ = f.attribute_fields do |a|
6
+ %span.fields{ "data-object-name" => f.object_name }
7
+ = a.attribute_select({:associations => %w(account tags activities emails addresses)}, :class => 'ransack_attribute')
8
+
9
+ = f.predicate_select options[:predicate_options] || {}, :class => 'ransack_predicate'
10
+
11
+ = f.value_fields do |v|
12
+ %span.fields.value{ 'data-object-name' => f.object_name }
13
+ = v.text_field :value
@@ -0,0 +1,12 @@
1
+ .fields{ 'data-object-name' => f.object_name }
2
+ %p
3
+ - key = (f.object_name =~ /[0]/) ? :advanced_search_group_first : :advanced_search_group_rest
4
+ = t(key, :combinator => f.combinator_select).html_safe
5
+
6
+ .filters
7
+ - f.object.build_condition unless f.object.conditions.any?
8
+ = f.condition_fields do |c|
9
+ = render 'ransack/condition_fields', :f => c, :options => options
10
+
11
+ %p
12
+ = link_to_add_fields t(:advanced_search_add_condition), f, :condition, options
@@ -0,0 +1,13 @@
1
+ #search
2
+ .tabs
3
+ %ul
4
+ %li
5
+ = link_to 'Basic Search', '#', :"data-search-form" => "basic_search", :class => (params[:q] ? "" : " active")
6
+ %li
7
+ = link_to 'Advanced Search', '#', :"data-search-form" => "advanced_search", :class => (!params[:q] ? "" : " active")
8
+
9
+ .search_form#basic_search{ hidden_if(params[:q]) }
10
+ = render "ransack/basic_search", :options => options
11
+
12
+ .search_form#advanced_search{ hidden_if(!params[:q]) }
13
+ = render "ransack/advanced_search", :options => options
@@ -0,0 +1,15 @@
1
+ require 'ransack_ui/view_helpers'
2
+
3
+ module RansackUI
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ initializer "ransack_ui.view_helpers" do
7
+ ActionView::Base.send :include, ViewHelpers
8
+ end
9
+
10
+ initializer :assets do
11
+ ::Rails.application.config.assets.precompile += %w( delete.png )
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,16 @@
1
+ require 'ransack/adapters/active_record/base'
2
+
3
+ module Ransack
4
+ module Adapters
5
+ module ActiveRecord
6
+ module Base
7
+ # Return array of attributes with [name, type]
8
+ # (Default to :string type for ransackers)
9
+ def ransackable_attributes(auth_object = nil)
10
+ columns.map{|c| [c.name, c.type] } +
11
+ _ransackers.keys.map {|k| [k, :string] }
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ require 'ransack/context'
2
+
3
+ module Ransack
4
+ Context.class_eval do
5
+ def self.ransackable_attribute?(str, klass)
6
+ klass.ransackable_attributes(auth_object).map(&:first).include? str
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,51 @@
1
+ require 'ransack/helpers/form_builder'
2
+
3
+ module Ransack
4
+ module Helpers
5
+ FormBuilder.class_eval do
6
+ def attribute_select(options = {}, html_options = {})
7
+ raise ArgumentError, "attribute_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
8
+ options[:include_blank] = true unless options.has_key?(:include_blank)
9
+ bases = [''] + association_array(options[:associations])
10
+ if bases.size > 1
11
+ @template.select(
12
+ @object_name, :name,
13
+ @template.grouped_options_for_select(attribute_collection_for_bases(bases)),
14
+ objectify_options(options), @default_options.merge(html_options)
15
+ )
16
+ else
17
+ collection = object.context.searchable_attributes(bases.first).map do |c|
18
+ [
19
+ attr_from_base_and_column(bases.first, c),
20
+ Translate.attribute(attr_from_base_and_column(bases.first, c), :context => object.context)
21
+ ]
22
+ end
23
+ @template.collection_select(
24
+ @object_name, :name, collection, :first, :last,
25
+ objectify_options(options), @default_options.merge(html_options)
26
+ )
27
+ end
28
+ end
29
+
30
+ def attribute_collection_for_bases(bases)
31
+ bases.map do |base|
32
+ begin
33
+ [
34
+ Translate.association(base, :context => object.context),
35
+ object.context.searchable_attributes(base).map do |c, type|
36
+ attribute = attr_from_base_and_column(base, c)
37
+ [
38
+ Translate.attribute(attribute, :context => object.context),
39
+ attribute,
40
+ {:'data-type' => type}
41
+ ]
42
+ end
43
+ ]
44
+ rescue UntraversableAssociationError => e
45
+ nil
46
+ end
47
+ end.compact
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module RansackUI
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ module RansackUI
2
+ module ViewHelpers
3
+ def ransack_search_form(options={})
4
+ render 'ransack/search', :options => options
5
+ end
6
+
7
+ def link_to_add_fields(name, f, type, options={})
8
+ new_object = f.object.send "build_#{type}"
9
+ fields = f.send("#{type}_fields", new_object, :child_index => "new_#{type}") do |builder|
10
+ render "ransack/#{type.to_s}_fields", :f => builder, :options => options
11
+ end
12
+ link_to name, nil, :class => "add_fields", "data-field-type" => type, "data-content" => "#{fields}"
13
+ end
14
+
15
+ def link_to_remove_fields(name, f)
16
+ link_to image_tag('delete.png', :size => '16x16', :alt => name), nil, :class => "remove_fields"
17
+ end
18
+ end
19
+ end
data/lib/ransack_ui.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "ransack_ui/version"
2
+ require "ransack_ui/rails/engine"
3
+
4
+ # Require ransack overrides
5
+ Dir.glob(File.expand_path('../ransack_ui/ransack_overrides/**/*.rb', __FILE__)) {|f| require f }
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ransack_ui/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "ransack_ui"
8
+ gem.version = RansackUI::VERSION
9
+ gem.authors = ["Nathan Broadbent"]
10
+ gem.email = ["nathan.f77@gmail.com"]
11
+ gem.description = "Framework for building a search UI with Ransack"
12
+ gem.summary = "UI Builder for Ransack"
13
+ gem.homepage = "https://github.com/ndbroadbent/ransack_ui"
14
+ gem.license = "MIT"
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ransack_ui
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nathan Broadbent
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Framework for building a search UI with Ransack
15
+ email:
16
+ - nathan.f77@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - app/assets/images/delete.png
27
+ - app/assets/javascripts/ransack/predicates.js.coffee
28
+ - app/assets/javascripts/ransack_ui_jquery.js
29
+ - app/assets/javascripts/ransack_ui_jquery/search_form.js.coffee
30
+ - app/assets/javascripts/ransack_ui_jquery/search_tabs.js.coffee
31
+ - app/views/ransack/_advanced_search.html.haml
32
+ - app/views/ransack/_basic_search.html.haml
33
+ - app/views/ransack/_condition_fields.html.haml
34
+ - app/views/ransack/_grouping_fields.html.haml
35
+ - app/views/ransack/_search.html.haml
36
+ - lib/ransack_ui.rb
37
+ - lib/ransack_ui/rails/engine.rb
38
+ - lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb
39
+ - lib/ransack_ui/ransack_overrides/context.rb
40
+ - lib/ransack_ui/ransack_overrides/helpers/form_builder.rb
41
+ - lib/ransack_ui/version.rb
42
+ - lib/ransack_ui/view_helpers.rb
43
+ - ransack_ui.gemspec
44
+ homepage: https://github.com/ndbroadbent/ransack_ui
45
+ licenses:
46
+ - MIT
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ segments:
58
+ - 0
59
+ hash: -1110025668031683009
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ segments:
67
+ - 0
68
+ hash: -1110025668031683009
69
+ requirements: []
70
+ rubyforge_project:
71
+ rubygems_version: 1.8.24
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: UI Builder for Ransack
75
+ test_files: []