forked from brightin/brightcontent
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from brightin/ransack-filtering
First shot at an automatic ransack implementation
- Loading branch information
Showing
17 changed files
with
137 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<% if filter_fields.present? %> | ||
<div class="panel-body"> | ||
<%= search_form_for ransack_search, class: "form-inline" do |form| %> | ||
<% filter_fields.each do |field| %> | ||
<div class="form-group"> | ||
<%= render_filter_field form, field %> | ||
</div> | ||
<% end %> | ||
<%= form.submit class: "btn btn-default btn-sm" %> | ||
<% end %> | ||
</div> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<div class="page-header"> | ||
<%= link_to "#{I18n.t('brightcontent.create_new', model: resource_class.model_name.human).capitalize}", polymorphic_url(resource_index_path, action: :new), class: "btn btn-primary pull-right" %> | ||
<h1><%= nominative_plural(resource_class).capitalize %></h1> | ||
</div> | ||
|
||
<%= render_if_exists "index_top" %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'active_support/concern' | ||
|
||
module Brightcontent | ||
module BaseControllerExt | ||
module Filtering | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
helper_method :ransack_search | ||
end | ||
|
||
def ransack_search | ||
@_ransack_search ||= end_of_association_chain.ransack(params[:q]) | ||
end | ||
|
||
def collection | ||
@_collection ||= ransack_search.result(distinct: true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Brightcontent | ||
module ViewLookup | ||
class FilterField < Abstract | ||
def render_default | ||
raise "invalid filter field: #{options[:field]}" unless field_name | ||
[ | ||
options[:form].label(:"#{field_name}_eq", options[:field].humanize), | ||
options[:form].select(:"#{field_name}_eq", select_options, {include_blank: true}, class: "form-control input-sm") | ||
].join(" ").html_safe | ||
end | ||
|
||
def field_type | ||
resource_class.columns_hash[options[:field]].try :type | ||
end | ||
|
||
private | ||
|
||
def field? | ||
resource_class.column_names.include? options[:field].to_s | ||
end | ||
|
||
def belongs_to_association? | ||
association.try :belongs_to? | ||
end | ||
|
||
def field_name | ||
if field? | ||
options[:field] | ||
elsif belongs_to_assocation? | ||
association.foreign_key | ||
end | ||
end | ||
|
||
def select_options | ||
if field? | ||
field_type == :boolean ? raw_options : raw_options.sort | ||
elsif belongs_to_assocation? | ||
association.klass.where(association.association_primary_key => raw_options).map do |record| | ||
[record, record[association.association_primary_key]] | ||
end | ||
end | ||
end | ||
|
||
def raw_options | ||
resource_class.uniq.pluck(field_name) | ||
end | ||
|
||
def association | ||
resource_class.reflect_on_association options[:field].to_sym | ||
end | ||
|
||
def resource_class | ||
view_context.send :resource_class | ||
end | ||
end | ||
end | ||
end |
1 change: 1 addition & 0 deletions
1
core/spec/dummy/app/controllers/brightcontent/blogs_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
class Brightcontent::BlogsController < Brightcontent::BaseController | ||
filter_fields %w[featured] | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,9 @@ | |
name "Blogname" | ||
body "Inhoud" | ||
featured false | ||
|
||
factory :featured_blog do | ||
featured true | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters