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

Create element in default position #103

Closed
csaura opened this issue Jan 16, 2014 · 6 comments
Closed

Create element in default position #103

csaura opened this issue Jan 16, 2014 · 6 comments

Comments

@csaura
Copy link
Contributor

csaura commented Jan 16, 2014

When I try create a new element in the default position create it at the bottom of the list.

Example:

*********************************
* Tasks
*********************************
* id
* name
* position , NOT NULL, DEFAULT 0
*********************************

class Task
 acts_as_list  top_of_list: 0, add_new_at: :bottom
end

# existing elements
task position: 0 id: 20
task position: 1 id: 35
task position: 2 id: 15
task position: 3 id: 14
task position: 4 id: 16
task position: 5 id: 17
task position: 6 id: 18

# create new element
task = Task.create(position: 0, name: 'I want to be at the top')

task.position => 7 # but I wanted to create it at position 0

How can I create a new element forcing the default position?

@csaura
Copy link
Contributor Author

csaura commented Jan 24, 2014

Do you think make sense to create a new method to force the top position?

@swanandp
Copy link
Contributor

:add_new_at :bottom will override position: 0

@swanandp
Copy link
Contributor

Actually no, what I said is not right.

@swanandp
Copy link
Contributor

Can you try

task = Task.build(name: 'I want to be at the top')
task.insert_at(0)

@danascheider
Copy link

I am having this same issue. :add_new_at does seem to override any sort of manual positioning. In my case, I have:

class Task 
  belongs_to :task_list
  acts_as_list scope: :task_list, add_new_at: :top    # top position in this case is 1
end

I'm trying to add slightly more complicated logic for default list positions:

  • By default, most tasks are created at position 1
  • If a task's status is changed to 'complete', it should go to the bottom of the list
  • If a task is created with status 'complete', it should be the first complete task

I have the first 2 under control; the 3rd one is causing me the same problem csaura is having. I've tried effecting the behavior through before_create callbacks, after_create callbacks, and by modifying the model's :create! method. None of these has had any impact whatsoever. I also tried what you suggested in your last post, and that didn't change anything either.

@brendon
Copy link
Owner

brendon commented Apr 17, 2016

Joining in late, but this sounds like two different scopes to me.

acts_as_list scope: [:task_list, :status], add_new_at: :top

or something like that. Still that wouldn't allow for the complex logic about where in the list an item in the complete status scope should go when it joins that scope. Callbacks are probably the answer, or manually specifying the position after checking the existing entries.

I'll close this for now since it's had no action for a while, but I'm happy to discuss it further if you want.

@brendon brendon closed this as completed Apr 17, 2016
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

4 participants