Module: ActiveRecord::Acts::MaterializedPath::ClassMethods

Defined in:
lib/acts_as_materialized_path.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_materialized_path(options = {}) ⇒ Object



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/acts_as_materialized_path.rb', line 27

def acts_as_materialized_path(options = {})
  conf = {
    :delimiter => '.',
    :base => 10,
    :column => 'materialized_path',
    :places => 3,
  }
  conf.update(options) if options.is_a?(Hash)

  unless conf[:delimiter].length == 1 &&
      (' '..'/') === conf[:delimiter]
    raise InvalidDelimiter
  end

  unless (2..36) === conf[:base]
    raise InvalidBase
  end

  conf[:adapter] =
    ActiveRecord::Base.connection.adapter_name

  #configuration settings
  write_inheritable_attribute :mp_delimiter, conf[:delimiter]
  class_inheritable_reader :mp_delimiter

  write_inheritable_attribute :mp_base, conf[:base]
  class_inheritable_reader :mp_base

  write_inheritable_attribute :mp_column, conf[:column]
  class_inheritable_reader :mp_column

  write_inheritable_attribute :mp_places, conf[:places]
  class_inheritable_reader :mp_places

  #sql helpers
  write_inheritable_attribute :mp_like, "#{mp_column} like ?"
  class_inheritable_reader :mp_like

  write_inheritable_attribute :mp_eq, "#{mp_column} = ?"
  class_inheritable_reader :mp_eq

  write_inheritable_attribute :mp_gt, "#{mp_column} > ?"
  class_inheritable_reader :mp_gt

  write_inheritable_attribute :mp_gte, "#{mp_column} >= ?"
  class_inheritable_reader :mp_gte

  write_inheritable_attribute :mp_lt, "#{mp_column} < ?"
  class_inheritable_reader :mp_lt

  write_inheritable_attribute :mp_asc, "#{mp_column} ASC"
  class_inheritable_reader :mp_asc

  write_inheritable_attribute :mp_desc, "#{mp_column} DESC"
  class_inheritable_reader :mp_desc

  limit_string = '~' # This only works with and asci collating sequence
  limit_string = 'Z'*(mp_places+1) # This works with UTF-8

  write_inheritable_attribute :mp_ancestor_limit,
  case conf[:adapter]
  when 'MySQL'
    "concat(#{mp_column}, '#{limit_string}')"
  when 'SQLServer'
    "#{mp_column} + '#{limit_string}'"
  else # ANSI SQL Syntax
    "#{mp_column} || '#{limit_string}'"
  end
  class_inheritable_reader :mp_ancestor_limit

  write_inheritable_attribute :mp_between,
  "? between #{mp_column} and #{mp_ancestor_limit}"
  class_inheritable_reader :mp_between

  #path manipulation fu
  write_inheritable_attribute :mp_regexp,
  Regexp.new("[[:alnum:]]{#{conf[:places]}}\\#{conf[:delimiter]}$")
  class_inheritable_reader :mp_regexp


  include ActiveRecord::Acts::MaterializedPath::InstanceMethods
  extend ActiveRecord::Acts::MaterializedPath::SingletonMethods

  #before_create :before_create_callback

  attr_protected conf[:column].to_sym


  #if parent set, save as child
  #if sibling set, save as sibling
  #else save as root
  attr_accessor :mp_parent_id_for_save
  attr_accessor :mp_sibling_id_for_save

  #this mucks up emacs indenting, so watch out for that
  class_eval <<-EOV
    def #{mp_column}=(newpath)
      raise InvalidAssignment
    end
  EOV

end