Skip to content

Commit

Permalink
Merge branch 'devin/issues/2'
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoss committed Nov 2, 2014
2 parents c546479 + d76dc47 commit 6ca1cbe
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
9 changes: 5 additions & 4 deletions lib/treeify.rb
Expand Up @@ -13,17 +13,18 @@ module Treeify
belongs_to :parent,
class_name: self,
foreign_key: "parent_id"

class_attribute :cols
scope :roots, -> { where(parent_id: nil) }
scope :tree_for, ->(instance) { where("#{table_name}.id IN (#{tree_sql_for(instance)})").order("#{table_name}.id") }
scope :tree_for_ancestors, ->(instance) { where("#{table_name}.id IN (#{tree_sql_for_ancestors(instance)})").order("#{table_name}.id") }

end

module ClassMethods

def config(hash = {})
# apparently columns is a reserved word in rails
self.cols = hash[:cols]

def tree_config(hash = {})
self.cols = hash[:cols]
end

def tree_sql(instance)
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Expand Up @@ -18,7 +18,7 @@

class Node < ActiveRecord::Base
include Treeify
config({cols: [:name]})
tree_config({cols: [:name]})

validates_uniqueness_of :name
validates_uniqueness_of :parent_id, :scope=> :id
Expand Down
7 changes: 4 additions & 3 deletions spec/treeify_spec.rb
Expand Up @@ -2,6 +2,10 @@

describe Treeify do
describe "Initialization" do
it "has the correct config values" do
expect(Node.cols).to eq([:name])
end

it "is set up correctly" do
expect(Node.table_name).to eq("nodes")
expect(Node.cols).to eq([:name])
Expand Down Expand Up @@ -118,9 +122,6 @@
}
])
end



end
end

0 comments on commit 6ca1cbe

Please sign in to comment.