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

Problem with reordering scoped list items #154

Closed
mcmegavolt opened this issue Feb 5, 2015 · 6 comments
Closed

Problem with reordering scoped list items #154

mcmegavolt opened this issue Feb 5, 2015 · 6 comments

Comments

@mcmegavolt
Copy link

Can you help me guys to figure out how to reorder list items within scope, here my models:

class Plan < ActiveRecord::Base
  # main association 
  has_many :items, -> { order("position ASC") },  dependent: :destroy
  # scoped associations
  has_many :draft_items, ->{ where(draft: true).order("position ASC") }
  has_many :final_items, ->{ where(draft: false).order("position ASC") }
end

class Item < ActiveRecord::Base
  belongs_to :plan
  acts_as_list scope: :plan
end

So, I need reorder list items within draft_items and final_items separately.

When I try to do this:

plan.draft_items.find(3).insert_at(2)

It makes global items reordering outside my scope among all plan items.

How to resolve this? Thanks!

@swanandp
Copy link
Contributor

swanandp commented Feb 6, 2015

@brendon @philippfranke Any chance you can take a look? I am going to be away for a while.

@brendon
Copy link
Owner

brendon commented Feb 8, 2015

Hi @mcmegavolt, acts_as_list removes any existing scoping before applying its own scope:

https://github.com/swanandp/acts_as_list/blob/master/lib/acts_as_list/active_record/acts/list.rb#L224

You'd have to utilise the :scope => feature to scope your list. I think the scope can be something like:

acts_as_list scope: :plan, :draft

I hope that helps.

@mcmegavolt
Copy link
Author

Thank you for responding!

I need sort items separately, with draft = true and draft = false but never for all plan items, only for those two cases.

Plan has many Items but I want to sort item.draft: true/false separately.

I use jQuery for sorting two tables (Plan items where draft is true and false) on one page by dragging rows and send Ajax request to sort them.

So, I need to sort only items with darft = true or draft = false,

Please, explain me how to do that.

Thank you again!

@brendon
Copy link
Owner

brendon commented Feb 9, 2015

Thanks @mcmegavolt, that will work well with the scope feature of acts_as_list. You're basically scoping the list on both the parent :plan and also the :draft column. So aal will maintain a separate list for each plan's draft and non-draft items. You might have to do `:scope => [:plan_id, :draft]' but just experiment. The scoping code is at the beginning of the aal source code and you can see how it tries to interpret different scope definitions.

@mcmegavolt
Copy link
Author

Yes! :scope => [:plan_id, :draft] is working!
I sincerely thank you for your help!
You are doing a good job, thanks!

@brendon
Copy link
Owner

brendon commented Feb 9, 2015

You're most welcome! :)

Brendon Muir

On Tue, Feb 10, 2015 at 7:41 AM, Aleksandr notifications@github.com wrote:

Yes! :scope => [:plan_id, :draft] is working!
I sincerely thank you for your help!
You are doing a good job, thanks!


Reply to this email directly or view it on GitHub
#154 (comment)
.

@brendon brendon closed this as completed Feb 9, 2015
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

No branches or pull requests

3 participants