Module: Nitro::Scaffold::ClassMethods

Included in:
Nitro::Scaffold
Defined in:
lib/nitro/scaffold.rb

Instance Method Summary collapse

Instance Method Details

#enchant(*classes) ⇒ Object

‘Enchant’ a class (typically a managed class). A collection of useful methods are magically added to the class and/or the class instances.

to_s to_href to_link to_edit_href to_admin_href

– to_xxx is used instead of xxx in an attempt to avoid colisions with user defined methods. ++



31
32
33
34
35
# File 'lib/nitro/scaffold.rb', line 31

def enchant *classes
  for c in classes
    enchant_class c
  end
end

#enchant_class(klass) ⇒ Object

– Actually enchant the given class. Override this method to customize for your application. The string calculation code is deliberatly dynamic to work with Ruby’s OO features. ++



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
# File 'lib/nitro/scaffold.rb', line 44

def enchant_class klass
  # to_s
        
  if klass.instance_methods.include? 'title'
    define_instance_method klass, :to_s, %{ title }, force = true      
  elsif klass.instance_methods.include? 'name'
    define_instance_method klass, :to_s, %{ name }, force = true      
  end

  # to_href
  # ex: blog/articles/23
  
  define_instance_method klass, :to_href, %{
    "\#{self.class.ann.self.controller.mount_path}/\#{self.class.name.demodulize.underscore.pluralize}/\#{oid}"
  }

  # to_link
  # ex: <a href="blog/articles/23">The article's title</a>
  
  define_instance_method klass, :to_link, %{
    %|<a href="\#{to_href}">\#{to_s}</a>|
  }
                
  # to_edit_href
  # ex: admin/articles/edit/23

  define_instance_method klass, :to_admin_href, %{
    "\#{AdminController.mount_path}/\#{self.class.name.underscore.gsub(/::/, '_').pluralize}/edit/\#{oid}"
  }

  if defined? AdminController
    # to_admin_href
    # ex: admin/articles/list

    define_instance_method klass, :to_admin_href, %{
      "\#{AdminController.mount_path}/\#{self.class.name.underscore.gsub(/::/, '_').pluralize}/list"
    }
                  
    # to_admin_href
    # ex: admin/articles/list

    define_class_method klass, :to_admin_href, %{
      "\#{AdminController.mount_path}/\#{self.name.underscore.gsub(/::/, '_').pluralize}/list"
    }
  end
end