Module: N::Scaffolding::SingletonMethods

Defined in:
lib/nitro/scaffold.rb

Instance Method Summary collapse

Instance Method Details

#scaffold(klass, options = {}) ⇒ Object

Enchant the caller with a number of default methods. Override the automatically generated methods as needed.



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
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/nitro/scaffold.rb', line 24

def scaffold(klass, options = {})
	
	oid = options[:oid] || 'oid'
	name = options[:name] || N::Inflector.name(klass.name)
	list_name = options[:list_name] || N::Inflector.plural_name(name)
	suffix = "_#{name}"

	# Add methods to the scaffolded class.
	
	klass.module_eval <<-"end_eval", __FILE__, __LINE__
		
		def view_uri
			"view#{suffix}?oid=\#\{@oid\}"
		end
		
	end_eval
	
	# Add methods to the service.
	
	if options[:index]
		module_eval <<-"end_eval", __FILE__, __LINE__
			
			def index
				list#{suffix}
			end
			
		end_eval
	end

	module_eval <<-"end_eval", __FILE__, __LINE__
		
		# TODO: add pager support here!

		def list#{suffix}
			@#{list_name} = #{klass}.all('ORDER BY oid')
		end
		
		def view#{suffix}
			@#{name} = #{klass}[@context['#{oid}']]
		end
		
		def save#{suffix}
		end
		
		def del#{suffix}
			#{klass}.delete(@context['#{oid}'])
		end
		alias_method :delete#{suffix}, :del#{suffix}
		
	end_eval
	
end