-
-
Notifications
You must be signed in to change notification settings - Fork 810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow ransacker to have its arguments #513
Allow ransacker to have its arguments #513
Conversation
Enable passing arguments to Ransackers
Thank you, @sineed. Would you like to update the change log and the wiki about this? I'll admit I'm hesitant about the name |
rename the test and ransacker to hopefully make the intent and API more explicit, wrap line at 80 characters. Follow-up to #513. `ransacker_args` is a bit longer but the tradeoff in being more self-explanatory and explicit and less cryptic seems worthwhile.
@sineed thanks. The test and wiki example query generates the following SQL query, which does not seem to be valid SQL.
Could you create an example that generates valid SQL? |
@sineed @jonatack How would this be used in a = sort_link @q, :read_items, 'Only Items Ive read' ransacker :read_items, args: [:parent, :ransacker_args] do |parent, args|
# Need args to have the current_user id
end which I want to pass over the current_user id, is that possible with this? The wiki only does not make it clear on how to pass an argument like this |
@jonatack @railsdevmatt |
@sineed - Ok so i have a forum with topics # forums controller
def show
@forum = Forum.find(params[:id])
@q = @forum.topics.ransack(params[:q])
@topics = @q.result.page(params[:page])
end
# view show.html.haml
- unless @topics.empty?
%ul
%li= sort_link @q, :read_topics, 'Only topics Ive read'
- @topics.each do |topic|
...
... I want to pass in the current_user to the read_topics ransacker that i created and filter only the read topics. I do know this is possible without ransack, but figured if there was a way to accomplish this with a ransacker then I would give it a go. |
@railsdevmatt Am I right if your topics have a link_to 'Only topics Ive read', forum_path(id: @forum.id, q: { readers_user_id: current_user.id }) |
@sineed - actually the relationship is like this # topic model
class Topic < ActiveRecord::Base
has_many :topic_reads, dependent: :destroy
has_many :subscribers, through: :topic_subscriptions class TopicRead < ActiveRecord::Base
belongs_to :topic
belongs_to :reader, class_name: "User", foreign_key: "reader_id" class TopicSubscription < ActiveRecord::Base
belongs_to :topic
belongs_to :subscriber, -> { where "users.email != '' AND users.email IS NOT NULL" }, class_name: "Contact", foreign_key: "subscriber_id" |
The sort_link is for setting the Any filtering should be happening in your controller action (where you have ready access to current_user). |
@sineed @jonatack Love this feature! If I were constructing a ransack query in my controller like so (by "manually" merging together attribute names with predicates):
And I wanted to pass arguments to my ransacker for
Is it possible to pass I've been playing around trying to properly include it in my hash but keep getting an undefined method error ransacker_args for the sort node. Since the documentation provides only one example of passing arguments to a ransacker it's a bit unclear if what I'm trying to do is even possible. |
This is related to #508 but I have another use case:
In my project I have Customer and Deal models. Customers have many Deals and each deal has
amount
attribute. I want to find customers which have made deals with total amount more than specified value for the last week. So my ransacker is:And I can easily filter:
The problem occurs when I want to change period of time. Say, for last month period I need to create new ransacker. What if I need to dynamically specify this period?
So with changes in this PR the following ransacker can be created:
And I can use this ransacker: