Class: Deli::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/deli/controller.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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
  options         = args.extract_options!
  @params         = []
  @params << param!(:page, :type => :offset) unless options[:page] == false
  @params << param!(:sort, :type => :order, :default => options[:sort]) unless options[:sort] == false
  @params << param!(:limit, :type => :limit, :default => options[:limit] || config.per_page) unless options[: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(options = {})
  (options[: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, options = {})
  model.model_type(key, options)
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, options = {})
  @params << param!(key, options)
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, options = {})
  "::Deli::Adapters::#{find_adapter(options)}::#{find_type(key.to_s, options).to_s.camelize}".constantize.new(
    key,
    options.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