Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit f184fff23320c2565ffb3e710ffa205a38992ea6 0 parents
@ndbroadbent authored
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
7 Gemfile
@@ -0,0 +1,7 @@
+source 'https://rubygems.org'
+
+# Include rake in Gemfile so that `bundle exec rake` doesn't raise an error
+gem 'rake', :group => :test
+
+# Specify your gem's dependencies in ransack_ui.gemspec
+gemspec
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2012 Nathan Broadbent
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 README.md
@@ -0,0 +1,27 @@
+# Ransack UI
+
+Provides HTML templates and JavaScript to build a fully functional
+advanced search form using Ransack.
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'ransack_ui'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install ransack_ui
+
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
1  Rakefile
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
BIN  app/assets/images/delete.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 app/assets/javascripts/ransack/predicates.js.coffee
@@ -0,0 +1,38 @@
+window.Ransack ?= {}
+
+Ransack.predicates =
+ eq: 'not_eq'
+ cont: 'not_cont'
+ matches: 'does_not_match'
+ start: 'not_start'
+ end: 'not_end'
+ present: 'blank'
+ null: 'not_null'
+ lt: 'gteq'
+ gt: 'lteq'
+ in: 'not_in'
+ true: 'false'
+
+# Setup supported predicates for each column type.
+Ransack.type_predicates = {}
+((o, f) -> f.call o) Ransack.type_predicates, ->
+ @text = @string = ['eq', 'cont', 'matches', 'start', 'end', 'present', 'in']
+ @boolean = ['true']
+ @integer = @float = @decimal = ['eq', 'null', 'lt', 'gt', 'in']
+ @date = @datetime = @time = ['eq', 'null', 'lt', 'gt']
+
+# Setup input field types for each predicate
+Ransack.predicate_inputs = {}
+((o, f) -> f.call o) Ransack.predicate_inputs, ->
+ @cont = @matches = @start = @end = @in = 'string'
+ @present = @null = @true = false
+ @eq = @gt = @lt = (type) ->
+ switch type
+ when 'string','text' then 'string'
+ when 'integer','float','decimal' then 'numeric'
+ when 'date','datetime','time' then type
+ else false # Hide for unhandled types.
+
+# Use a tags input for 'in' if Select2 is available
+if Select2?
+ Ransack.predicate_inputs.in = 'tags'
3  app/assets/javascripts/ransack_ui_jquery.js
@@ -0,0 +1,3 @@
+//= require ransack/predicates
+//= require ransack_ui_jquery/search_form
+//= require ransack_ui_jquery/search_tabs
113 app/assets/javascripts/ransack_ui_jquery/search_form.js.coffee
@@ -0,0 +1,113 @@
+(($) ->
+ $.widget 'ransack.search_form',
+ options: {}
+
+ _create: ->
+ el = this.element
+ el.on 'click', '.add_fields', $.proxy(this.add_fields, this)
+ el.on 'click', '.remove_fields', $.proxy(this.remove_fields, this)
+ el.on 'change', 'select.ransack_predicate', $.proxy(this.predicate_changed, this)
+ el.on 'change', 'select.ransack_attribute', $.proxy(this.attribute_changed, this)
+
+ # Set up Select2 on select lists in .filters
+ this.init_select2(this.element.find('.filters'))
+
+ # show spinner and disable the form when the search is underway
+ el.find("form input:submit").click $.proxy(this.form_submit, this)
+
+ # Fire change event for any existing selects.
+ el.find(".filters select").change()
+
+ # For basic search, remove placeholder text on focus, restore on blur
+ $('#query').focusin (e) ->
+ $(this).data('placeholder', $(this).attr('placeholder')).attr('placeholder', '')
+ $('#query').focusout (e) ->
+ $(this).attr('placeholder', $(this).data('placeholder'))
+
+ predicate_changed: (e) ->
+ target = $(e.currentTarget)
+ value_el = $('input#' + target.attr('id').slice(0, -1) + "v_0_value")
+ if target.val() in ["true", "false", "blank", "present", "null", "not_null"]
+ value_el.val("true")
+ value_el.hide()
+ else
+ unless value_el.is(":visible")
+ value_el.val("")
+ value_el.show()
+
+ attribute_changed: (e) ->
+ target = $(e.currentTarget)
+ predicate_select = this.element.find('select#' + target.attr('id').slice(0, -8) + "p")
+ previous_val = predicate_select.val()
+ type = target.find('option:selected').data('type')
+
+ # Build array of supported predicates
+ available = predicate_select.data['predicates']
+
+ predicates = Ransack.type_predicates[type] || []
+ predicates = $.map predicates, (p) -> [p, Ransack.predicates[p]]
+
+ # Remove all predicates, and add any supported predicates
+ predicate_select.find('option').each (i, o) -> $(o).remove()
+
+ $.each available, (i, p) ->
+ [val, label] = [p[0], p[1]]
+ if val in predicates
+ predicate_select.append $('<option value='+val+'>'+label+'</option>')
+
+ # Select first predicate if current selection is invalid
+ predicate_select.select2('val', previous_val)
+
+ return true
+
+ form_submit: (e) ->
+ $("#loading").show()
+ this.element.css({ opacity: 0.4 })
+ $('div.list').html('')
+ true
+
+ add_fields: (e) ->
+ target = $(e.currentTarget)
+ type = target.data("fieldType")
+ content = target.data("content")
+ new_id = new Date().getTime()
+ regexp = new RegExp('new_' + type, 'g')
+ container = target.closest('p')
+ container.before content.replace(regexp, new_id)
+ this.init_select2 container.prev()
+ # Fire change event on any new selects.
+ container.prev().find("select").change()
+ false
+
+ remove_fields: (e) ->
+ target = $(e.currentTarget)
+ container = target.closest('.fields')
+ if (container.siblings().length > 1)
+ container.remove()
+ else
+ container.parent().closest('.fields').remove()
+ false
+
+ init_select2: (container) ->
+ if Select2?
+ # Store current predicates in data attribute
+ predicate_select = container.find('select.ransack_predicate')
+ unless predicate_select.data['predicates']
+ predicates = []
+ predicate_select.find('option').each (i, o) ->
+ $o = $(o)
+ predicates.push [$o.val(), $o.text()]
+ predicate_select.data['predicates'] = predicates
+
+ container.find('select.ransack_predicate').select2
+ width: '130px'
+ formatNoMatches: (term) ->
+ "No predicates found"
+
+ container.find('select.ransack_attribute').select2
+ width: '220px'
+ placeholder: "Select a Field"
+ allowClear: true
+ formatSelection: (object, container) ->
+ $(object.element).parent().attr('label') + ': ' + object.text
+) jQuery
31 app/assets/javascripts/ransack_ui_jquery/search_tabs.js.coffee
@@ -0,0 +1,31 @@
+(($) ->
+ $ ->
+ # Search tabs
+ # -----------------------------------------------------
+ activate_search_form = (search_form) ->
+ # Hide all
+ $('#search .search_form').hide()
+ $('#search .tabs li a').removeClass('active')
+ # Show selected
+ $('#' + search_form).show()
+ $('a[data-search-form=' + search_form + ']').addClass('active')
+ # Run search for current query
+ switch search_form
+ when 'basic_search'
+ query_input = $('#basic_search input#query')
+ if !query_input.is('.defaultTextActive')
+ value = query_input.val()
+ else
+ value = ""
+ crm.search(value, window.controller)
+ $('#filters').enable() # Enable filters panel (if present)
+
+ when 'advanced_search'
+ $("#advanced_search form input:submit").click()
+ $('#filters').disable() # Disable filters panel (if present)
+
+ return
+ $("#search .tabs a").click ->
+ activate_search_form($(this).data('search-form'))
+
+) jQuery
12 app/views/ransack/_advanced_search.html.haml
@@ -0,0 +1,12 @@
+= search_form_for search, :url => url_for(:action => :index), :html => {:method => :get, :class => "advanced_search"}, :remote => true do |f|
+
+ = f.grouping_fields do |g|
+ = render 'ransack/grouping_fields', :f => g, :options => options
+
+ %p
+ = link_to_add_fields t(:advanced_search_add_group), f, :grouping, options
+
+ %p
+ = hidden_field_tag :distinct, '1'
+ = hidden_field_tag :page, '1'
+ = f.submit t(:advanced_search_submit)
2  app/views/ransack/_basic_search.html.haml
@@ -0,0 +1,2 @@
+%div{ :style => "margin: 0px 0px 6px 0px" }
+ = text_field_tag('query', @current_query, :size => 32, :placeholder => "Search #{controller_name}")
13 app/views/ransack/_condition_fields.html.haml
@@ -0,0 +1,13 @@
+.fields.condition{ "data-object-name" => f.object_name }
+ %p
+ = link_to_remove_fields t(:advanced_search_remove_condition), f
+
+ = f.attribute_fields do |a|
+ %span.fields{ "data-object-name" => f.object_name }
+ = a.attribute_select({:associations => %w(account tags activities emails addresses)}, :class => 'ransack_attribute')
+
+ = f.predicate_select options[:predicate_options] || {}, :class => 'ransack_predicate'
+
+ = f.value_fields do |v|
+ %span.fields.value{ 'data-object-name' => f.object_name }
+ = v.text_field :value
12 app/views/ransack/_grouping_fields.html.haml
@@ -0,0 +1,12 @@
+.fields{ 'data-object-name' => f.object_name }
+ %p
+ - key = (f.object_name =~ /[0]/) ? :advanced_search_group_first : :advanced_search_group_rest
+ = t(key, :combinator => f.combinator_select).html_safe
+
+ .filters
+ - f.object.build_condition unless f.object.conditions.any?
+ = f.condition_fields do |c|
+ = render 'ransack/condition_fields', :f => c, :options => options
+
+ %p
+ = link_to_add_fields t(:advanced_search_add_condition), f, :condition, options
13 app/views/ransack/_search.html.haml
@@ -0,0 +1,13 @@
+#search
+ .tabs
+ %ul
+ %li
+ = link_to 'Basic Search', '#', :"data-search-form" => "basic_search", :class => (params[:q] ? "" : " active")
+ %li
+ = link_to 'Advanced Search', '#', :"data-search-form" => "advanced_search", :class => (!params[:q] ? "" : " active")
+
+ .search_form#basic_search{ hidden_if(params[:q]) }
+ = render "ransack/basic_search", :options => options
+
+ .search_form#advanced_search{ hidden_if(!params[:q]) }
+ = render "ransack/advanced_search", :options => options
5 lib/ransack_ui.rb
@@ -0,0 +1,5 @@
+require "ransack_ui/version"
+require "ransack_ui/rails/engine"
+
+# Require ransack overrides
+Dir.glob(File.expand_path('../ransack_ui/ransack_overrides/**/*.rb', __FILE__)) {|f| require f }
15 lib/ransack_ui/rails/engine.rb
@@ -0,0 +1,15 @@
+require 'ransack_ui/view_helpers'
+
+module RansackUI
+ module Rails
+ class Engine < ::Rails::Engine
+ initializer "ransack_ui.view_helpers" do
+ ActionView::Base.send :include, ViewHelpers
+ end
+
+ initializer :assets do
+ ::Rails.application.config.assets.precompile += %w( delete.png )
+ end
+ end
+ end
+end
16 lib/ransack_ui/ransack_overrides/adapters/active_record/base.rb
@@ -0,0 +1,16 @@
+require 'ransack/adapters/active_record/base'
+
+module Ransack
+ module Adapters
+ module ActiveRecord
+ module Base
+ # Return array of attributes with [name, type]
+ # (Default to :string type for ransackers)
+ def ransackable_attributes(auth_object = nil)
+ columns.map{|c| [c.name, c.type] } +
+ _ransackers.keys.map {|k| [k, :string] }
+ end
+ end
+ end
+ end
+end
9 lib/ransack_ui/ransack_overrides/context.rb
@@ -0,0 +1,9 @@
+require 'ransack/context'
+
+module Ransack
+ Context.class_eval do
+ def self.ransackable_attribute?(str, klass)
+ klass.ransackable_attributes(auth_object).map(&:first).include? str
+ end
+ end
+end
51 lib/ransack_ui/ransack_overrides/helpers/form_builder.rb
@@ -0,0 +1,51 @@
+require 'ransack/helpers/form_builder'
+
+module Ransack
+ module Helpers
+ FormBuilder.class_eval do
+ def attribute_select(options = {}, html_options = {})
+ raise ArgumentError, "attribute_select must be called inside a search FormBuilder!" unless object.respond_to?(:context)
+ options[:include_blank] = true unless options.has_key?(:include_blank)
+ bases = [''] + association_array(options[:associations])
+ if bases.size > 1
+ @template.select(
+ @object_name, :name,
+ @template.grouped_options_for_select(attribute_collection_for_bases(bases)),
+ objectify_options(options), @default_options.merge(html_options)
+ )
+ else
+ collection = object.context.searchable_attributes(bases.first).map do |c|
+ [
+ attr_from_base_and_column(bases.first, c),
+ Translate.attribute(attr_from_base_and_column(bases.first, c), :context => object.context)
+ ]
+ end
+ @template.collection_select(
+ @object_name, :name, collection, :first, :last,
+ objectify_options(options), @default_options.merge(html_options)
+ )
+ end
+ end
+
+ def attribute_collection_for_bases(bases)
+ bases.map do |base|
+ begin
+ [
+ Translate.association(base, :context => object.context),
+ object.context.searchable_attributes(base).map do |c, type|
+ attribute = attr_from_base_and_column(base, c)
+ [
+ Translate.attribute(attribute, :context => object.context),
+ attribute,
+ {:'data-type' => type}
+ ]
+ end
+ ]
+ rescue UntraversableAssociationError => e
+ nil
+ end
+ end.compact
+ end
+ end
+ end
+end
3  lib/ransack_ui/version.rb
@@ -0,0 +1,3 @@
+module RansackUI
+ VERSION = "0.0.1"
+end
19 lib/ransack_ui/view_helpers.rb
@@ -0,0 +1,19 @@
+module RansackUI
+ module ViewHelpers
+ def ransack_search_form(options={})
+ render 'ransack/search', :options => options
+ end
+
+ def link_to_add_fields(name, f, type, options={})
+ new_object = f.object.send "build_#{type}"
+ fields = f.send("#{type}_fields", new_object, :child_index => "new_#{type}") do |builder|
+ render "ransack/#{type.to_s}_fields", :f => builder, :options => options
+ end
+ link_to name, nil, :class => "add_fields", "data-field-type" => type, "data-content" => "#{fields}"
+ end
+
+ def link_to_remove_fields(name, f)
+ link_to image_tag('delete.png', :size => '16x16', :alt => name), nil, :class => "remove_fields"
+ end
+ end
+end
20 ransack_ui.gemspec
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+require 'ransack_ui/version'
+
+Gem::Specification.new do |gem|
+ gem.name = "ransack_ui"
+ gem.version = RansackUI::VERSION
+ gem.authors = ["Nathan Broadbent"]
+ gem.email = ["nathan.f77@gmail.com"]
+ gem.description = "Framework for building a search UI with Ransack"
+ gem.summary = "UI Builder for Ransack"
+ gem.homepage = "https://github.com/ndbroadbent/ransack_ui"
+ gem.license = "MIT"
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.require_paths = ["lib"]
+end
Please sign in to comment.
Something went wrong with that request. Please try again.