Module: AnafHabtm::ActiveRecord

Defined in:
lib/anaf_habtm/anaf_active_record.rb

Instance Method Summary collapse

Instance Method Details

#anaf_habtm(association, 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
# File 'lib/anaf_habtm/anaf_active_record.rb', line 27

def anaf_habtm(association, options={})
  ar_opts = options[:ar_options] ||= {}
  klass = ar_opts[:class_name] ||= association.to_s.singularize.camelize
  class_eval do
    has_and_belongs_to_many association.to_sym, ar_opts
  end
  find_line = "obj = obj ||= #{klass}.#{make_scope_string(options[:find])}.first" if options.has_key?(:find)
  association = association.to_s.underscore.tableize
  scope_string =
  class_eval <<END
      def #{association}_attributes=(attr_hash)
        obj_coll = []
        attr_hash.each_value do |obj_attr|
          next if obj_attr["_destroy"] == "1"
          obj = #{klass}.find(obj_attr["id"]) if obj_attr.has_key?("id")
          obj_attr = obj_attr.reject_keys ["id", "_destroy"]
          #{find_line}
          if obj && obj.update_attributes(obj_attr)
            obj_coll << obj
          elsif (obj = #{klass}.new(obj_attributes)) && obj.save
            obj_coll << obj
          end
        end
        self.#{association} = obj_coll
      end
END
end

#association_text(association, opts = {}) ⇒ Object



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
# File 'lib/anaf_habtm/anaf_active_record.rb', line 55

def association_text(association, opts={})
  raise "Must give a name for text_association" unless opts.has_key?(:name)
  class_eval do
    if opts.has_key?(:class_name)
      klass = opts[:class_name].constantize
    else
      klass = association.to_s.singularize.camelize.constantize
    end
    define_method "#{association.to_s}_text=", lambda{ |text|
      return if text.empty?
      coll = StringReader.new.read_items(text) do |name, commentary|
        a = klass.undecorate(name) if klass.methods.include?("undecorate")
        if opts.has_key?(:scope)
          obj = self.send(association).scopes[opts[:scope]].call(name).first
        end
        params = {opts[:name]=>name}
        params[opts[:commentary]] = commentary if opts.has_key?(:commentary)
        obj = obj ||= klass.new
        obj = klass.find(obj) unless obj.new_record?
        obj.decoration = a if a
        obj.attributes = params
        obj.save
        obj
      end
      self.send("#{association.to_s}=", coll)
    }

    define_method "#{association.to_s}_text", lambda{
      StringReader.new.write_items(self.send(association.to_s)) do |item|
        name = item.send(opts[:name])
        name = item.decorate(name) if item.methods.include?("decorate")
        [name, opts.has_key?(:commentary) ? item.send(opts[:commentary]) : ""]
      end
    }
  end
end

#autocomplete_format(&block) ⇒ Object



4
5
6
7
8
9
10
11
# File 'lib/anaf_habtm/anaf_active_record.rb', line 4

def autocomplete_format(&block)
  class_eval do
    @autocomplete_block = block
    def self.to_autocomplete(items)
        items.map{|t| @autocomplete_block.call(t)}.to_json
    end
  end
end

#condition(cond) ⇒ Object



155
156
157
# File 'lib/anaf_habtm/anaf_active_record.rb', line 155

def condition(cond)
   search_columns.map{|c| "trim(lower(#{c})) #{cond}"}.join(" or ")
end

#equals_condition(str) ⇒ Object



146
147
148
# File 'lib/anaf_habtm/anaf_active_record.rb', line 146

def equals_condition(str)
  where(condition("= '#{str.downcase}'"))
end

#like_condition(str) ⇒ Object



142
143
144
# File 'lib/anaf_habtm/anaf_active_record.rb', line 142

def like_condition(str)
  where(condition("ilike '%#{str}%'"))
end

#lookup(params) ⇒ Object



133
134
135
136
137
138
139
140
# File 'lib/anaf_habtm/anaf_active_record.rb', line 133

def lookup(params)
  str = params[:id]
  if str.match(/\D/)
    named(str)
  else
    find(str)
  end
end

#make_scope_string(find_opts) ⇒ Object

Needs to call scopes on a class and pass it values that are

pulled from a hash called obj_attr


15
16
17
18
19
20
21
22
23
24
25
# File 'lib/anaf_habtm/anaf_active_record.rb', line 15

def make_scope_string(find_opts)
  _scopes = []
  find_opts.each{ |scope, attr_name|
    if attr_name.class == Hash
      _scopes << "#{scope.to_s}(obj_attr[#{name.first[0].to_s}][#{name.first[1].to_s}])"
    else
      _scopes << "#{scope.to_s}(obj_attr[#{name.to_s}])"
    end
  }
  _scopes.join(".")
end

#named(str) ⇒ Object



151
152
153
# File 'lib/anaf_habtm/anaf_active_record.rb', line 151

def named(str)
  with_name(str).first
end

#named_association(member, attribute, opts = {}) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/anaf_habtm/anaf_active_record.rb', line 92

def named_association(member, attribute, opts={})
  member = member.to_s
  klass = (opts.has_key?(:class_name) ? opts[:class_name] : member.to_s.singularize.camelize).constantize
  attribute = attribute.to_s
  if opts.has_key?(:create)
    class_eval do
      define_method "#{member}_#{attribute}=", lambda{|value|
        return if value.blank?
        obj = klass.named(value)
        obj = obj ||= klass.create(attribute => value)
        self.send("#{member}=", obj)
      }
    end
  else
    class_eval do
      define_method "#{member}_#{attribute}=", lambda{|value|
        self.send("#{member}=",klass.named(value)) unless value.blank?
      }
    end
  end
  class_eval "def #{member}_#{attribute};
     #{member}.#{attribute} if #{member};
   end;"
end

#search_on(*cols) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/anaf_habtm/anaf_active_record.rb', line 117

def search_on(*cols)
  class_eval "def self.search_columns; #{cols.map{|t| t.to_s}.to_ary.inspect}; end;"
  class_eval do
    scope :search, lambda{|str|
      items = like_condition(str.downcase)
      if scopes.has_key?(:search_mod)
        items = items.search_mod
      end
      items
    }
    scope :with_name, lambda{|str|
      equals_condition(str.downcase).limit(1)
    }
  end
end