File: set_validator.rb

package info (click to toggle)
ruby-awesome-nested-set 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 172 kB
  • sloc: ruby: 1,044; makefile: 2
file content (63 lines) | stat: -rw-r--r-- 1,993 bytes parent folder | download | duplicates (2)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
module CollectiveIdea #:nodoc:
  module Acts #:nodoc:
    module NestedSet #:nodoc:
      class SetValidator

        def initialize(model)
          @model = model
          @scope = model.all
          @parent = arel_table.alias('parent')
        end

        def valid?
          query.count == 0
        end

        private

        attr_reader :model, :parent
        attr_accessor :scope

        delegate :parent_column_name, :primary_column_name, :primary_key, :left_column_name, :right_column_name, :arel_table,
          :quoted_table_name, :quoted_parent_column_full_name, :quoted_left_column_full_name, :quoted_right_column_full_name, :quoted_left_column_name, :quoted_right_column_name, :quoted_primary_column_name,
        :to => :model

        def query
          join_scope
          filter_scope
        end

        def join_scope
          join_arel = arel_table.join(parent, Arel::Nodes::OuterJoin).on(parent[primary_column_name].eq(arel_table[parent_column_name]))
          self.scope = scope.joins(join_arel.join_sources)
        end

        def filter_scope
          self.scope = scope.where(
                                   bound_is_null(left_column_name).
                                   or(bound_is_null(right_column_name)).
                                   or(left_bound_greater_than_right).
                                   or(parent_not_null.and(bounds_outside_parent))
                                   )
        end

        def bound_is_null(column_name)
          arel_table[column_name].eq(nil)
        end

        def left_bound_greater_than_right
          arel_table[left_column_name].gteq(arel_table[right_column_name])
        end

        def parent_not_null
          arel_table[parent_column_name].not_eq(nil)
        end

        def bounds_outside_parent
          arel_table[left_column_name].lteq(parent[left_column_name]).or(arel_table[right_column_name].gteq(parent[right_column_name]))
        end

      end
    end
  end
end