Class: Deli::Controller
- Inherits:
-
Object
- Object
- Deli::Controller
- Defined in:
- lib/deli/controller.rb
Instance Attribute Summary (collapse)
-
- (Object) model_name
Returns the value of attribute model_name.
-
- (Object) params
Returns the value of attribute params.
Instance Method Summary (collapse)
- - (Object) config
- - (Object) find(key)
- - (Object) find_adapter(options = {})
- - (Object) find_type(key, options = {})
-
- (Controller) initialize(*args, &block)
constructor
not clean at all, couples the classes, but just added quickly to accomplish it.
- - (Object) keys
-
- (Object) match(key, options = {})
handles the following:.
- - (Object) model
- - (Object) param!(key, options = {})
- - (Object) query
- - (Object) query_class
- - (Object) render(params)
Constructor Details
- (Controller) initialize(*args, &block)
not clean at all, couples the classes, but just added quickly to accomplish it. needs refactoring.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/deli/controller.rb', line 6 def initialize(*args, &block) self.model_name = args.shift = args. @params = [] @params << param!(:page, :type => :offset) unless [:page] == false @params << param!(:sort, :type => :order, :default => [:sort]) unless [:sort] == false @params << param!(:limit, :type => :limit, :default => [:limit] || config.per_page) unless [:limit] == false instance_eval(&block) self end |
Instance Attribute Details
- (Object) model_name
Returns the value of attribute model_name
3 4 5 |
# File 'lib/deli/controller.rb', line 3 def model_name @model_name end |
- (Object) params
Returns the value of attribute params
3 4 5 |
# File 'lib/deli/controller.rb', line 3 def params @params end |
Instance Method Details
- (Object) config
17 18 19 |
# File 'lib/deli/controller.rb', line 17 def config Deli.configuration end |
- (Object) find(key)
40 41 42 |
# File 'lib/deli/controller.rb', line 40 def find(key) query.find(key) end |
- (Object) find_adapter(options = {})
64 65 66 |
# File 'lib/deli/controller.rb', line 64 def find_adapter( = {}) ([:adapter] || config.default_adapter).to_s.camelize end |
- (Object) find_type(key, options = {})
52 53 54 |
# File 'lib/deli/controller.rb', line 52 def find_type(key, = {}) model.model_type(key, ) end |
- (Object) keys
48 49 50 |
# File 'lib/deli/controller.rb', line 48 def keys params.map(&:key) end |
- (Object) match(key, options = {})
handles the following:
match :created_at # figures out attribute from table definition
match :created_by # figures out that it's on the 'users' table, and to add the join
match :name, :to => {:vendor => :name} # maps to 'vendors.name' column.
if 'vendor' is polymorphic, it will figure it out from the association reflection.
can only handle one level deep for now, no {:vendor => {:role => :name}}
match :name, :to => :by_vendor_name # maps to named scope
you can also write your own join:
match :name, :joins => "INNER JOIN products ON products.id = bookmarks.id"
32 33 34 |
# File 'lib/deli/controller.rb', line 32 def match(key, = {}) @params << param!(key, ) end |
- (Object) model
56 57 58 |
# File 'lib/deli/controller.rb', line 56 def model @model ||= "::Deli::Adapters::#{config.default_adapter.to_s.camelize}::Model".constantize.new(model_name) end |
- (Object) param!(key, options = {})
68 69 70 71 72 73 |
# File 'lib/deli/controller.rb', line 68 def param!(key, = {}) "::Deli::Adapters::#{find_adapter()}::#{find_type(key.to_s, ).to_s.camelize}".constantize.new( key, .reverse_merge(:model_name => self.model_name, :parser => self) ) end |
- (Object) query
44 45 46 |
# File 'lib/deli/controller.rb', line 44 def query @query ||= query_class.new(self) end |
- (Object) query_class
60 61 62 |
# File 'lib/deli/controller.rb', line 60 def query_class @query_class ||= "::Deli::Adapters::#{config.default_adapter.to_s.camelize}::Query".constantize end |
- (Object) render(params)
36 37 38 |
# File 'lib/deli/controller.rb', line 36 def render(params) query.render(params) end |