I am using rolify gem for authorization. When i create a new user dynamically through UI, my users_roles (in my case role_user_map) table insert is not happening. Is there a way to override build_query method? Please, guide me with this. Thank You. `
users_controller.rb
def index
store_list = #query_stores.split(" ").join("','")
profile_list = #profiles.map {|role| role.role_name}.join("','")
if params[:search_by].present?
search_by = params[:search_by]
if current_user.admin? || current_user.radial_readonly?
#users = User.distinct.joins('LEFT JOIN stores_users ON dim_user.user_id = stores_users.user_id')
.joins('LEFT JOIN stores ON stores.id = stores_users.store_id')
.joins('LEFT JOIN clients_users ON dim_user.user_id = clients_users.user_id')
.joins('LEFT JOIN clients ON clients_users.client_id = clients.id')
.joins(:DIM_ROLE)
.where('dim_user.full_name LIKE :search OR dim_user.email LIKE :search OR DIM_ROLE.role_name LIKE :search OR clients.code LIKE :search OR stores.code LIKE :search', search: "%#{search_by}%")
else
#users = User.distinct.joins(:store_user_assignments)
.joins(:stores)
.where("stores.code IN ('#{store_list}')")
.joins('LEFT JOIN clients_users ON dim_user.user_id = clients_users.user_id')
.joins('LEFT JOIN clients ON clients_users.client_id = clients.id')
.joins(:DIM_ROLE)
.where("DIM_ROLE.role_name IN ('#{profile_list}')")
.where('dim_user.full_name LIKE :search OR dim_user.email LIKE :search OR DIM_ROLE.role_name LIKE :search OR clients.code LIKE :search OR stores.code LIKE :search', search: "%#{search_by}%")
end
else
if current_user.admin? || current_user.radial_readonly?
#users = User.distinct.joins('LEFT JOIN stores_users ON dim_user.user_id = stores_users.store_id')
else
#users = User.distinct.joins(:store_user_assignments).joins(:DIM_ROLE).where("DIM_ROLE.role_name IN ('#{profile_list}')").joins(:stores).where("stores.code IN ('#{store_list}')")
end
end
if current_user.pmt_ptl_accnt_manager?
#users = #users.find_all {|user| !user.admin? and current_user.client_list.include? user.client_list}
end
# added #users_total to get the total users count before paginate
=begin
#users_total = #users
#users = #users.paginate(:per_page => params[:per_page] || 5, :page => params[:page])
#users_per_page = [{"name" => "5 per page", "id" => "5"},
{"name" => "10 per page", "id" => "10"},
{"name" => "15 per page", "id" => "15"},
{"name" => "20 per page", "id" => "20"}]
=end
end
`
Related
i am trying to delete from list but when i am trying this it is getting deleted from database
#course = Course.find(params[:id])
#search = Lesson.search(params[:q])
#lessons = #search.result.paginate(:page => params[:page], :per_page => 10)
#search.build_condition if #search.conditions.empty?
#course.lessons.each do |lesson|
#lessons.each do |l|
if lesson.id == l.id
#lessons.delete(l)
end
end
end
I am getting this error: delete_all doesn't support limit scope
Thanking you
Delete is an ActiveRecord method. I assume you don't want to delete it from the database but from the result list. You can do it like this:
#course.lessons.each do |lesson|
#lesson.reject { |l| l.id == lesson.id }
end
I'm running Spree 1.3.1 and I'm trying to customize the Taxon show page.
I would like it to return the products contained inside the current Taxon, eventually filtered by a property or by an option value.
For example let's say that I'm seeing the Taxon of an underwear collection.
I'd like to filter the products shown, by providing a certain size (option_type).
In this case I should list only products that have variants with the requested size.
I would like also to be able to filter the products by the "fit" property.
Filtering by the slip fit, I should be able to list only products inside the current Taxon that have the required property.
This is the Taxon controller show action:
Spree::TaxonsController.class_eval do
def show
#taxon = Spree::Taxon.find_by_permalink!(params[:id])
return unless #taxon
#searcher = Spree::Config.searcher_class.new(params)
#searcher.current_user = try_spree_current_user
#searcher.current_currency = current_currency
#products = #searcher.retrieve_products
respond_with(#taxon)
end
end
How should I modify it to fit my needs?
I partially solved the question.
I found out that I need to leave the controller as it is, the magic is done in the lib/spree/product_filters.rb file where I added this new product filter:
if Spree::Property.table_exists?
Spree::Product.add_search_scope :fit_any do |*opts|
conds = opts.map {|o| ProductFilters.fit_filter[:conds][o]}.reject {|c| c.nil?}
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
Spree::Product.with_property("fit").where(scope)
end
def ProductFilters.fit_filter
fit_property = Spree::Property.find_by_name("fit")
fits = Spree::ProductProperty.where(:property_id => fit_property).pluck(:value).uniq
pp = Spree::ProductProperty.arel_table
conds = Hash[*fits.map { |b| [b, pp[:value].eq(b)] }.flatten]
{ :name => "Fits",
:scope => :fit_any,
:conds => conds,
:labels => (fits.sort).map { |k| [k, k] }
}
end
end
Then I added the new filter to the Taxon model decorator with this:
Spree::Taxon.class_eval do
def applicable_filters
fs = []
fs << Spree::Core::ProductFilters.fit_filter if Spree::Core::ProductFilters.respond_to?(:fit_filter)
fs
end
end
Still I haven't found out how to create a filter for variants that have a specific option value.
You talk about filtering on numerical value, I've written a filter for option ranges:
def ProductFilters.ov_range_test(range1, range2)
ov = Arel::Table.new("spree_option_values")
cast = Arel::Nodes::NamedFunction.new "CAST", [ ov[:presentation].as("integer")]
comparaisons = cast.in(range1..range2)
comparaisons
end
Spree::Product.add_search_scope :screenSize_range_any do |*opts|
conds = opts.map {|o| Spree::ProductFilters.screenSize_filter[:conds][o]}.reject {|c| c.nil?}
scope = conds.shift
conds.each do |new_scope|
scope = scope.or(new_scope)
end
option_values=Spree::OptionValue.where(scope).joins(:option_type).where(OptionType.table_name => {:name => "tailleEcran"}).pluck("#{OptionValue.table_name}.id")
Spree::Product.where("#{Product.table_name}.id in (select product_id from #{Variant.table_name} v left join spree_option_values_variants ov on ov.variant_id = v.id where ov.option_value_id in (?))", option_values)
end
def ProductFilters.screenSize_filter
conds = [ [ "20p ou moins",ov_range_test(0,20)],
[ "20p - 30p",ov_range_test(20,30)],
[ "30p - 40p" ,ov_range_test(30,40)],
[ "40p ou plus",ov_range_test(40,190)]]
{ :name => "taille",
:scope => :screenSize_range_any,
:options => :tailleEcran,
:conds => Hash[*conds.flatten],
:labels => conds.map {|k,v| [k,k]}
}
end
You can see this one too, for discrete specific values:
https://gist.github.com/Ranger-X/2511088
I am fairly still new to ruby on rails and don't fully understand why I am getting the following error:
undefined local variable or method `user' for #<StatisticsController:0xb9a20d0>
The code:
class StatisticsController < ApplicationController
before_filter :authenticate, :only => [:index]
def index
#title = "Statistics"
#projects = Project.all
#data = []
Project.all.each do |project|
projdata = { 'name' => project.project_name.to_s,
'values' => [] }
['Pre-Sales','Project','Fault Fixing','Support' ].each do |taskname|
record = Effort.sum( :hours,
:joins => {:project_task => {:efforts => :user}},
:conditions => { "project_tasks.efforts.user_id" => user.id,
"project_tasks.project_id" => project.id,
"project_tasks.task_name" => taskname } )
projdata[ 'values' ].push( record )
end
#data.push( projdata )
end
end
end
Update
class StatisticsController < ApplicationController
before_filter :authenticate, :only => [:index]
def index
#title = "Statistics"
#projects = Project.all
#data = []
User.all.each do |user|
projdata = { 'name' => user.user_id.to_s,
'values' => [] }
['Pre-Sales','Project','Fault Fixing','Support' ].each do |taskname|
user = User.all
record = Effort.sum( :hours,
:joins => {:project_task => {:efforts => :user}},
:conditions => { "project_tasks.efforts.user_id" => user.id,
"project_tasks.project_id" => project.id,
"project_tasks.task_name" => taskname } )
projdata[ 'values'].push( record )
end
#data.push( projdata )
end
end
end
In string :conditions => { "project_tasks.efforts.user_id" => user.id, you call id for user object, but it is not instantiated in code above.
Your update doesn't loop over the users at all; user is now a collection of all the users. You need to iterate over the users if you want to get individual statistics for individual users.
Are you using devise? Use current_user instead of user.
Fix of your code:
User.all.each do |user|
projdata = { 'name' => user.user_id.to_s,
'values' => [] }
['Pre-Sales','Project','Fault Fixing','Support' ].each do |taskname|
record = Effort.sum( :hours,
:joins => {:project_task => {:efforts => :user}},
:conditions => { "project_tasks.efforts.user_id" => user.id,
"project_tasks.project_id" => project.id,
"project_tasks.task_name" => taskname } )
projdata[ 'values'].push( record )
end
#data.push( projdata )
end
So: removed the rogue user=User.all :)
Question: in 1 place you write user.user_id and in the other you write user.id. Is that correct?
I have a call on my posts_controller.rb index action:
#articles = Article.order("id desc")
I now want to be able to order by:
date
id
some_counter_attribute
My querystring will have sort=date/id/count like:
www.example.com/articles/?sort=date
How should I implement this in my controller now? Should I just use if statements?
if params[:sort] == "date"
#articles = Article.order("created_at desc")
elsif params[:sort] == "count"
#articles = ...
..
Or is there a better way?
Should this logic be in the controller or Model ideally?
Try this:
class ArticlesController
def index
#articles = Article.order(sort_order)
end
private
def sort_order
##sort_order ||= {
"date" => "created_at DESC",
"id" => "id DESC",
"comments" => "comment_count ASC"
}
##sort_order[params[:sort]]
end
end
Off course there are gems for doing this sort of things:
MetaSearch
SearchLogic
Straightforward approach could be:
#articles = Article.order("#{params[:sort]} desc")
But for "date" you have to sort by created_at. So try this:
mylist = {"date" => "created_at",
"id" => "id",
"counter" => "some_counter_attribute"}
#articles = Article.order("#{mylist[params[:sort]]} desc")
I have an index view that lists all of the tags for my Entry and Message models. I would like to only show the tags for Entries in this view. I'm using acts-as-taggable-on.
Tags Controller:
def index
#letter = params[:letter].blank? ? 'a' : params[:letter]
#tagged_entries = Tagging.find_all_by_taggable_type('Entry').map(&:taggable)
#title = "Tags"
if params[:letter] == '#'
#data = Tag.find(#tagged_entries, :conditions => ["name REGEXP ?",
"^[^a-z]"], :order => 'name', :select => "id, name")
else
#data = Tag.find(#tagged_entries, :conditions => ["name LIKE ?",
"#{params[:letter]}%"], :order => 'name', :select => "id, name")
end
respond_to do |format|
flash[:notice] = 'We are currently in Beta. You may experience errors.'
format.html
end
end
tags#index:
<% #data.each do |t| %>
<div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
<% end %>
I want to show only the taggable type 'Entry' in the view.
Any ideas? Thank you for reading my question.
SECOND EDIT:
Tags Controller:
def index
#title = "Tags"
#letter = params[:letter].blank? ? 'a' : params[:letter]
#taggings = Tagging.find_all_by_taggable_type('Entry', :include => [:tag, :taggable])
#tags = #taggings.map(&:tag).sort_by(&:name).uniq
#tagged_entries = #taggings.map(&:taggable)#.sort_by(&:id)#or whatever
if params[:letter] == '#'
#data = Tag.find(#tags, :conditions => ["name REGEXP ?",
"^[^a-z]"], :order => 'name', :select => "id, name")
else
#data = Tag.find(#tags, :conditions => ["name LIKE ?",
"#{params[:letter]}%"], :order => 'name', :select => "id, name")
end
respond_to do |format|
format.html
end
end
tags#index:
<% #data.each do |t| %>
<div class="tag"><%= link_to t.name.titleize, tag_path(t) %></div>
<% end %>
Max Williams' code works except when I click on my alphabetical pagination links. The error I'm getting [after I clicked on the G link of the alphabetical pagination] reads:
Couldn't find all Tags with IDs (77,130,115,...) AND (name LIKE 'G%') (found 9 results, but was looking for 129)
Can anyone point me in the right direction?
#taggings = Tagging.find_all_by_taggable_type('Entry', :include => [:tag, :taggable])
#tags = #taggings.map(&:tag).sort_by(&:name)
#tagged_entries = #taggings.map(&:taggable).sort_by(&:id)#or whatever