Class: Deli::Controller
- Inherits:
-
Object
- Object
- Deli::Controller
- Defined in:
- lib/deli/controller.rb
Instance Attribute Summary collapse
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#params ⇒ Object
Returns the value of attribute params.
Instance Method Summary collapse
- #config ⇒ Object
- #find(key) ⇒ Object
- #find_adapter(options = {}) ⇒ Object
- #find_type(key, options = {}) ⇒ Object
-
#initialize(*args, &block) ⇒ Controller
constructor
not clean at all, couples the classes, but just added quickly to accomplish it.
- #keys ⇒ Object
-
#match(key, options = {}) ⇒ Object
handles the following:.
- #model ⇒ Object
- #param!(key, options = {}) ⇒ Object
- #query ⇒ Object
- #query_class ⇒ Object
- #render(params) ⇒ Object
Constructor Details
#initialize(*args, &block) ⇒ Controller
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
#model_name ⇒ Object
Returns the value of attribute model_name.
3 4 5 |
# File 'lib/deli/controller.rb', line 3 def model_name @model_name end |
#params ⇒ Object
Returns the value of attribute params.
3 4 5 |
# File 'lib/deli/controller.rb', line 3 def params @params end |
Instance Method Details
#config ⇒ Object
17 18 19 |
# File 'lib/deli/controller.rb', line 17 def config Deli.configuration end |
#find(key) ⇒ Object
40 41 42 |
# File 'lib/deli/controller.rb', line 40 def find(key) query.find(key) end |
#find_adapter(options = {}) ⇒ Object
64 65 66 |
# File 'lib/deli/controller.rb', line 64 def find_adapter( = {}) ([:adapter] || config.default_adapter).to_s.camelize end |
#find_type(key, options = {}) ⇒ Object
52 53 54 |
# File 'lib/deli/controller.rb', line 52 def find_type(key, = {}) model.model_type(key, ) end |
#keys ⇒ Object
48 49 50 |
# File 'lib/deli/controller.rb', line 48 def keys params.map(&:key) end |
#match(key, options = {}) ⇒ Object
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 |
#model ⇒ Object
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 |
#param!(key, options = {}) ⇒ Object
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 |
#query ⇒ Object
44 45 46 |
# File 'lib/deli/controller.rb', line 44 def query @query ||= query_class.new(self) end |
#query_class ⇒ Object
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 |
#render(params) ⇒ Object
36 37 38 |
# File 'lib/deli/controller.rb', line 36 def render(params) query.render(params) end |