Post Meta

Bookmarks

  • Delicious
  • Digg
  • Reddit
  • Magnolia
  • Newsvine
  • Furl
  • Facebook
  • Technorati

I had to hunt down many snippets to make it working.

In the ActiveScaffold forum I’ve found this nice code, which almost works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 
Here's the model:
class Category < ActiveRecord::Base
has_many :children, :class_name  =>  'Category', :foreign_key  => 
'parent_id'
belongs_to :parent, :class_name  =>  'Category', :foreign_key  => 
'parent_id'
has_many :articles
acts_as_nested_set
validates_presence_of :title
after_create :setchild
def setchild
if self[:parent_id].nil?
return true
end
self.save
self.move_to_child_of self.parent
return true
end
end
And controller:
class CategoryController < ApplicationController
layout 'admin'
active_scaffold :category do |config|
config.columns[:title].set_link('<strong style="color: black; background-color: #a0ffff">nested</strong>', :parameters  => 
{:associations  =>  :children})
config.list.columns.exclude :children, :parent, :lft, :rgt
config.create.columns = :title, :description
config.update.columns = :title, :description
end
def conditions_for_collection
return ['parent_id IS NULL'] if !params.has_key?(:<strong style="color: black; background-color: #a0ffff">nested</strong>)
end
end

I had bugs in after_create and I found another snippet at another forum thread.

1
2
3
4
 
def after_create_save(record)
record.move_to_child_of record.parent.id unless record.parent.nil?
end

… and now it seems to be working in development mode. the full snippet is here:

  • in the model:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    class Help &lt; ActiveRecord::Base
    has_many :children, :class_name  =>  "Help", :foreign_key  =>  :parent_id
    belongs_to :parent, :class_name  =>  "Help", :foreign_key  =>  :parent_id
     
    acts_as_nested_set
     
    #after_create :set_child
     
    def after_create_save(record)
    record.move_to_child_of record.parent.id unless record.parent.nil?
    end
     
    end
  • in the controller:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
     
    class HelpsController &lt; ApplicationController
    layout nil
     
    active_scaffold :helps do |config|
    config.list.columns = [:name, :description, :ro]
    config.update.columns = [:name, :description, :ro]
    config.create.columns = [:name, :description, :ro]
    config.show.columns = [:name, :description, :ro]
     
    #config.columns[:name].set_link('nested', :parameters  =>  {:associations  =>  :children})
    config.nested.add_link "nested", [:children]
     
    list.sorting = {:name  =>  'ASC'}
     
    # Internationalization / localization
    config.label = Help.localized_model_name
    config.columns[:name].label = Help.human_attribute_name(:name)
    config.columns[:description].label = Help.human_attribute_name(:description)
    config.columns[:en].label = Help.human_attribute_name(:en)
    config.columns[:ro].label = Help.human_attribute_name(:ro)
    config.columns[:hu].label = Help.human_attribute_name(:hu)
    end
     
    def conditions_for_collection
    return ['parent_id IS NULL'] if !params.has_key?(:nested)
    end
    end


Related posts

Leave A Comment

+ -