ordered_tree 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.textile +8 -0
 - data/VERSION +1 -1
 - data/lib/ordered_tree.rb +10 -2
 - data/ordered_tree.gemspec +3 -2
 - data/spec/fixtures/category.rb +3 -0
 - data/spec/ordered_tree_spec.rb +18 -0
 - data/spec/spec_helper.rb +8 -2
 - metadata +5 -4
 
    
        data/README.textile
    CHANGED
    
    | 
         @@ -154,6 +154,14 @@ pre. def scope_condition 
     | 
|
| 
       154 
154 
     | 
    
         
             
              "site_id = #{site_id} AND user_login = '#{user_login}'"
         
     | 
| 
       155 
155 
     | 
    
         
             
            end
         
     | 
| 
       156 
156 
     | 
    
         | 
| 
      
 157 
     | 
    
         
            +
            h2. Making parent_id (the foreign key) point to something else
         
     | 
| 
      
 158 
     | 
    
         
            +
             
     | 
| 
      
 159 
     | 
    
         
            +
            If you want @parent_id@ to point to something else instead of the id, then supply @:primary_key => :alternate_id@ to the @ordered_tree@ method. This will only probably be useful in tandem with a scope:
         
     | 
| 
      
 160 
     | 
    
         
            +
             
     | 
| 
      
 161 
     | 
    
         
            +
            <pre>
         
     | 
| 
      
 162 
     | 
    
         
            +
            ordered_tree :primary_key => :relative_id, :scope => :account
         
     | 
| 
      
 163 
     | 
    
         
            +
            </pre>
         
     | 
| 
      
 164 
     | 
    
         
            +
             
     | 
| 
       157 
165 
     | 
    
         
             
            h2. Install
         
     | 
| 
       158 
166 
     | 
    
         | 
| 
       159 
167 
     | 
    
         
             
            gem 'ordered_tree'
         
     | 
    
        data/VERSION
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            0.1. 
     | 
| 
      
 1 
     | 
    
         
            +
            0.1.8
         
     | 
    
        data/lib/ordered_tree.rb
    CHANGED
    
    | 
         @@ -24,10 +24,18 @@ module OrderedTree #:nodoc: 
     | 
|
| 
       24 
24 
     | 
    
         
             
                self.ordered_tree_config ||= {}
         
     | 
| 
       25 
25 
     | 
    
         
             
                self.ordered_tree_config[:foreign_key] ||= :parent_id
         
     | 
| 
       26 
26 
     | 
    
         
             
                self.ordered_tree_config[:order] ||= :position
         
     | 
| 
      
 27 
     | 
    
         
            +
                self.ordered_tree_config[:primary_key] ||= :id
         
     | 
| 
       27 
28 
     | 
    
         
             
                self.ordered_tree_config.update(options) if options.is_a?(Hash)
         
     | 
| 
       28 
29 
     | 
    
         | 
| 
       29 
     | 
    
         
            -
                belongs_to :parent_node, 
     | 
| 
       30 
     | 
    
         
            -
             
     | 
| 
      
 30 
     | 
    
         
            +
                belongs_to :parent_node,
         
     | 
| 
      
 31 
     | 
    
         
            +
                  :class_name => self.name,
         
     | 
| 
      
 32 
     | 
    
         
            +
                  :foreign_key => ordered_tree_config[:foreign_key],
         
     | 
| 
      
 33 
     | 
    
         
            +
                  :primary_key => ordered_tree_config[:primary_key]
         
     | 
| 
      
 34 
     | 
    
         
            +
                has_many :child_nodes,
         
     | 
| 
      
 35 
     | 
    
         
            +
                  :class_name => self.name,
         
     | 
| 
      
 36 
     | 
    
         
            +
                  :foreign_key => ordered_tree_config[:foreign_key],
         
     | 
| 
      
 37 
     | 
    
         
            +
                  :primary_key => ordered_tree_config[:primary_key],
         
     | 
| 
      
 38 
     | 
    
         
            +
                  :order => ordered_tree_config[:order]
         
     | 
| 
       31 
39 
     | 
    
         
             
                scope :roots, lambda { |*args|
         
     | 
| 
       32 
40 
     | 
    
         
             
                  scope_condition = args[0]
         
     | 
| 
       33 
41 
     | 
    
         
             
                  where(scope_condition).where(self.ordered_tree_config[:foreign_key].to_sym => 0).order(self.ordered_tree_config[:order])
         
     | 
    
        data/ordered_tree.gemspec
    CHANGED
    
    | 
         @@ -5,11 +5,11 @@ 
     | 
|
| 
       5 
5 
     | 
    
         | 
| 
       6 
6 
     | 
    
         
             
            Gem::Specification.new do |s|
         
     | 
| 
       7 
7 
     | 
    
         
             
              s.name = %q{ordered_tree}
         
     | 
| 
       8 
     | 
    
         
            -
              s.version = "0.1. 
     | 
| 
      
 8 
     | 
    
         
            +
              s.version = "0.1.8"
         
     | 
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
              s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
         
     | 
| 
       11 
11 
     | 
    
         
             
              s.authors = ["Ramon Tayag"]
         
     | 
| 
       12 
     | 
    
         
            -
              s.date = %q{2011- 
     | 
| 
      
 12 
     | 
    
         
            +
              s.date = %q{2011-08-07}
         
     | 
| 
       13 
13 
     | 
    
         
             
              s.description = %q{Uses parent_id and position to create an ordered tree.}
         
     | 
| 
       14 
14 
     | 
    
         
             
              s.email = %q{ramon@tayag.net}
         
     | 
| 
       15 
15 
     | 
    
         
             
              s.extra_rdoc_files = [
         
     | 
| 
         @@ -34,6 +34,7 @@ Gem::Specification.new do |s| 
     | 
|
| 
       34 
34 
     | 
    
         
             
                "lib/ordered_tree/instance_methods/misc.rb",
         
     | 
| 
       35 
35 
     | 
    
         
             
                "lib/ordered_tree/instance_methods/tree.rb",
         
     | 
| 
       36 
36 
     | 
    
         
             
                "ordered_tree.gemspec",
         
     | 
| 
      
 37 
     | 
    
         
            +
                "spec/fixtures/category.rb",
         
     | 
| 
       37 
38 
     | 
    
         
             
                "spec/fixtures/page.rb",
         
     | 
| 
       38 
39 
     | 
    
         
             
                "spec/fixtures/person.rb",
         
     | 
| 
       39 
40 
     | 
    
         
             
                "spec/ordered_tree_spec.rb",
         
     | 
    
        data/spec/ordered_tree_spec.rb
    CHANGED
    
    | 
         @@ -6,6 +6,15 @@ describe OrderedTree do 
     | 
|
| 
       6 
6 
     | 
    
         
             
                @people = Person.all
         
     | 
| 
       7 
7 
     | 
    
         
             
              end
         
     | 
| 
       8 
8 
     | 
    
         | 
| 
      
 9 
     | 
    
         
            +
              describe "when a primary_key is supplied" do
         
     | 
| 
      
 10 
     | 
    
         
            +
                it "should use the parent_id to point to the primary_key instead of :id" do
         
     | 
| 
      
 11 
     | 
    
         
            +
                  c1 = Category.create(:alt_id => 10)
         
     | 
| 
      
 12 
     | 
    
         
            +
                  c2 = Category.create(:parent_id => 10)
         
     | 
| 
      
 13 
     | 
    
         
            +
                  c2.parent.should == c1
         
     | 
| 
      
 14 
     | 
    
         
            +
                  c1.children.should include(c2)
         
     | 
| 
      
 15 
     | 
    
         
            +
                end
         
     | 
| 
      
 16 
     | 
    
         
            +
              end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
       9 
18 
     | 
    
         
             
              describe "when a scope is supplied" do
         
     | 
| 
       10 
19 
     | 
    
         
             
                # This is especially important for working with root items that may belong to different accounts
         
     | 
| 
       11 
20 
     | 
    
         
             
                it "should only work within that scope" do
         
     | 
| 
         @@ -59,6 +68,15 @@ describe OrderedTree do 
     | 
|
| 
       59 
68 
     | 
    
         
             
                  page_4.parent.should == nil
         
     | 
| 
       60 
69 
     | 
    
         
             
                  page_5.position.should == 1
         
     | 
| 
       61 
70 
     | 
    
         
             
                  page_5.parent.should == nil
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
                  # delete a root item
         
     | 
| 
      
 73 
     | 
    
         
            +
                  page_1.destroy
         
     | 
| 
      
 74 
     | 
    
         
            +
             
     | 
| 
      
 75 
     | 
    
         
            +
                  # reload all
         
     | 
| 
      
 76 
     | 
    
         
            +
                  [page_2, page_4, page_5].map(&:reload).each do |page|
         
     | 
| 
      
 77 
     | 
    
         
            +
                    page.position.should == 1
         
     | 
| 
      
 78 
     | 
    
         
            +
                    page.parent.should be_nil
         
     | 
| 
      
 79 
     | 
    
         
            +
                  end
         
     | 
| 
       62 
80 
     | 
    
         
             
                end
         
     | 
| 
       63 
81 
     | 
    
         
             
              end
         
     | 
| 
       64 
82 
     | 
    
         | 
    
        data/spec/spec_helper.rb
    CHANGED
    
    | 
         @@ -6,6 +6,7 @@ require 'rspec' 
     | 
|
| 
       6 
6 
     | 
    
         
             
            require 'ordered_tree'
         
     | 
| 
       7 
7 
     | 
    
         
             
            require 'spec/fixtures/person'
         
     | 
| 
       8 
8 
     | 
    
         
             
            require 'spec/fixtures/page'
         
     | 
| 
      
 9 
     | 
    
         
            +
            require 'spec/fixtures/category'
         
     | 
| 
       9 
10 
     | 
    
         | 
| 
       10 
11 
     | 
    
         
             
            #Allow to connect to SQLite
         
     | 
| 
       11 
12 
     | 
    
         
             
            ActiveRecord::Base.establish_connection(
         
     | 
| 
         @@ -21,7 +22,7 @@ RSpec.configure do |config| 
     | 
|
| 
       21 
22 
     | 
    
         
             
            end
         
     | 
| 
       22 
23 
     | 
    
         | 
| 
       23 
24 
     | 
    
         
             
            def reset_database
         
     | 
| 
       24 
     | 
    
         
            -
              %W(people pages).each do |table_name|
         
     | 
| 
      
 25 
     | 
    
         
            +
              %W(people pages categories).each do |table_name|
         
     | 
| 
       25 
26 
     | 
    
         
             
                ActiveRecord::Base.connection.execute("DROP TABLE IF EXISTS '#{table_name}'")
         
     | 
| 
       26 
27 
     | 
    
         
             
              end
         
     | 
| 
       27 
28 
     | 
    
         
             
              ActiveRecord::Base.connection.create_table(:people) do |t|
         
     | 
| 
         @@ -36,6 +37,11 @@ def reset_database 
     | 
|
| 
       36 
37 
     | 
    
         
             
                t.string :name
         
     | 
| 
       37 
38 
     | 
    
         
             
                t.integer :person_id
         
     | 
| 
       38 
39 
     | 
    
         
             
              end
         
     | 
| 
      
 40 
     | 
    
         
            +
              ActiveRecord::Base.connection.create_table(:categories) do |t|
         
     | 
| 
      
 41 
     | 
    
         
            +
                t.integer :parent_id, :null => false, :default => 0
         
     | 
| 
      
 42 
     | 
    
         
            +
                t.integer :position
         
     | 
| 
      
 43 
     | 
    
         
            +
                t.integer :alt_id
         
     | 
| 
      
 44 
     | 
    
         
            +
              end
         
     | 
| 
       39 
45 
     | 
    
         
             
            end
         
     | 
| 
       40 
46 
     | 
    
         | 
| 
       41 
47 
     | 
    
         
             
            def ordered_tree(klass, *opts)
         
     | 
| 
         @@ -46,7 +52,7 @@ ensure 
     | 
|
| 
       46 
52 
     | 
    
         
             
            end
         
     | 
| 
       47 
53 
     | 
    
         | 
| 
       48 
54 
     | 
    
         
             
            # Test Tree
         
     | 
| 
       49 
     | 
    
         
            -
            # 
     | 
| 
      
 55 
     | 
    
         
            +
            #
         
     | 
| 
       50 
56 
     | 
    
         
             
            # We will be working with this tree through out the tests
         
     | 
| 
       51 
57 
     | 
    
         
             
            #
         
     | 
| 
       52 
58 
     | 
    
         
             
            # people[0]
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,13 +1,13 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification 
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: ordered_tree
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version 
         
     | 
| 
       4 
     | 
    
         
            -
              hash:  
     | 
| 
      
 4 
     | 
    
         
            +
              hash: 11
         
     | 
| 
       5 
5 
     | 
    
         
             
              prerelease: 
         
     | 
| 
       6 
6 
     | 
    
         
             
              segments: 
         
     | 
| 
       7 
7 
     | 
    
         
             
              - 0
         
     | 
| 
       8 
8 
     | 
    
         
             
              - 1
         
     | 
| 
       9 
     | 
    
         
            -
              -  
     | 
| 
       10 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 9 
     | 
    
         
            +
              - 8
         
     | 
| 
      
 10 
     | 
    
         
            +
              version: 0.1.8
         
     | 
| 
       11 
11 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       12 
12 
     | 
    
         
             
            authors: 
         
     | 
| 
       13 
13 
     | 
    
         
             
            - Ramon Tayag
         
     | 
| 
         @@ -15,7 +15,7 @@ autorequire: 
     | 
|
| 
       15 
15 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       16 
16 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       17 
17 
     | 
    
         | 
| 
       18 
     | 
    
         
            -
            date: 2011- 
     | 
| 
      
 18 
     | 
    
         
            +
            date: 2011-08-07 00:00:00 +08:00
         
     | 
| 
       19 
19 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       21 
21 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     | 
| 
         @@ -179,6 +179,7 @@ files: 
     | 
|
| 
       179 
179 
     | 
    
         
             
            - lib/ordered_tree/instance_methods/misc.rb
         
     | 
| 
       180 
180 
     | 
    
         
             
            - lib/ordered_tree/instance_methods/tree.rb
         
     | 
| 
       181 
181 
     | 
    
         
             
            - ordered_tree.gemspec
         
     | 
| 
      
 182 
     | 
    
         
            +
            - spec/fixtures/category.rb
         
     | 
| 
       182 
183 
     | 
    
         
             
            - spec/fixtures/page.rb
         
     | 
| 
       183 
184 
     | 
    
         
             
            - spec/fixtures/person.rb
         
     | 
| 
       184 
185 
     | 
    
         
             
            - spec/ordered_tree_spec.rb
         
     |