Class: Admin::Param

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/admin/param.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



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
# File 'app/models/admin/param.rb', line 24

def method_missing(method, *args)
    method_name = method.to_s
    super(method, *args)

    rescue NoMethodError

    if method_name =~ /=$/
      puts method_name
         puts args.first

     var_name = method_name.gsub('=', '')
     value = args.first
     o= items.where(:name=>var_name).first
   if o==nil
     o=items.new
     o.name= var_name
   end
    o.value = value
    o.save
   #retrieve a value
   else
     (eval "items.where(:name=>'#{method_name}')").first.value
   end

end

Class Method Details

.method_missing(method, *args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/models/admin/param.rb', line 10

def self.method_missing(method, *args)
  method_name = method.to_s
  super(method, *args)

rescue NoMethodError
  if (param=self.find_by_name(method_name)) == nil   && method_name !='to_ary'
    param=self.create!(:name=>method_name)
  else
    param
  end
  return param
end