V
V
vadimstroganov2015-12-03 17:28:58
Ruby on Rails
vadimstroganov, 2015-12-03 17:28:58

Output list and tree view for Ancesty?

Hello!
Using a bunch of gems: rails_admin + ancesrty + rails_admin_nestable
I'm trying to make a dropdown list when creating a page to specify the parent.
in Page.rb:

def self.ancestry_options(items)
  result = []
  items.map do |item, sub_items|
    result << [yield(item), item.id]
    result += ancestry_options(sub_items) {|i| "#{'-' * i.depth} #{i.title}" }
  end
  result
end

In rails_admin.rb:
field :ancestry, :enum do
  enum do
    Page::ancestry_options(Page.unscoped.arrange(:order => 'id')) { |i| "#{'-' * i.depth} #{i.title}" }
  end
end

It should look something like this:
5319bc21b98c4acaaf290b35705032bf.png
But!
When creating a new page, in the ancestry field, he simply writes the page id (from the drop-down list)
For example, page id:
root_page - 1
Videos - 2
Video 1 - 3

Then when creating a new page with the Videos ancestor, the ancestry field should take the value "1/2", and not just "2"
You can add a callback after creating/updating the record and make the field look like "1/2", but then after that , for example, if you go into editing the page that we just created, we should see the already selected parent page - Videos, but there is a void, since it is now stored not "2", but "1/2"
Question: is it possible to somehow then to form a drop-down list, cut the pedigree to the last parent?
e.g. "1/2/5" => "5"
or "1/2" => 2
and after saving it back to "2" => "1/2"

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question