Skip to content
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 creating aliases for ransack attributes #623

Merged
merged 1 commit into from
Dec 19, 2015
Merged

Allow creating aliases for ransack attributes #623

merged 1 commit into from
Dec 19, 2015

Conversation

rzane
Copy link
Contributor

@rzane rzane commented Dec 9, 2015

Ransack is great, but the query strings can get crazy pretty quickly, especially when querying associations. It would be really great to have aliases for ransack attributes.

It seems like people want this feature built into Ransack. See:

This PR allows you to do this:

class Author < ActiveRecord::Base
  # Columns: (first_name, last_name)
end

class Post < ActiveRecord::Base
  # Columns: (title, author_id)
  belongs_to :author
  ransack_alias :name, :title
  ransack_alias :author, :author_first_name_or_author_last_name
end

puts Post.ransack(name_start: 'Fun').result.to_sql
puts Post.ransack(author_cont: 'Ray').result.to_sql
SELECT "posts".* FROM "posts"
WHERE ("posts"."title" LIKE 'Fun%')

SELECT "posts".* FROM "posts"
LEFT OUTER JOIN "authors" ON "authors"."id" = "posts"."author_id"
WHERE ("authors"."first_name" LIKE '%Ray%' OR "authors"."last_name" LIKE '%Ray%')

@@ -33,6 +33,14 @@ def quote_column_name name
end

module ClassMethods
def _ransack_aliases
@_ransack_alises ||= {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a typo here. It did not cause the specs to fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely a typo, but that variable is always being accessed through it's reader, which is why the tests pass anyway.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, memoization. Thanks.

@jonatack
Copy link
Contributor

Thanks for the PR! Will test it locally soon.

@rzane
Copy link
Contributor Author

rzane commented Dec 16, 2015

Hey @jonatack, I fixed that typo. Have you had a chance to take a look?

jonatack added a commit that referenced this pull request Dec 19, 2015
Allow creating aliases for ransack attributes.
@jonatack jonatack merged commit 1de3453 into activerecord-hackery:master Dec 19, 2015
@jonatack
Copy link
Contributor

LGTM, thanks @rzane. Could you update the README and Change Log to document this change?

@rzane
Copy link
Contributor Author

rzane commented Dec 19, 2015

Sure. I'll do that right now.

@jonatack
Copy link
Contributor

Great.

jonatack added a commit that referenced this pull request Dec 19, 2015
jonatack added a commit that referenced this pull request Dec 20, 2015
[skip ci] Document the use of ransack_alias. Follow-up to #623.
@lauramosher
Copy link

I have been excited for this change since I spotted it a month ago. Do you have an ETA for when this will be released?

@jonatack
Copy link
Contributor

Hi @lauramosher, you can use all the latest features with the master branch.

We do need to do a release soon but there are a couple of issues to iron out before...

@lauramosher
Copy link

@jonatack Thank you for the reply! Unfortunately, I am unable to use the master branch in this instance, but greatly look forward to the release. Keep up the awesome work.

@laserlemon
Copy link
Contributor

Looks great! Anything I can do to help move this into a release? Thank you! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants