class TheGrid::Api

Attributes

options[R]
relation[R]

Public Class Methods

new(relation) click to toggle source
# File lib/the_grid/api.rb, line 5
def initialize(relation)
  @relation = relation
  @options = { :delegated_commands => {} }
end

Public Instance Methods

compose!(params) click to toggle source
# File lib/the_grid/api.rb, line 14
def compose!(params)
  configure(params).fetch(:cmd).each do |cmd|
    @relation = run_command!(cmd, params) unless command(cmd).batch?
  end
end
delegate(commands) click to toggle source
# File lib/the_grid/api.rb, line 10
def delegate(commands)
  options[:delegated_commands].merge! commands.stringify_keys
end
run_command!(name, params) click to toggle source
# File lib/the_grid/api.rb, line 20
def run_command!(name, params)
  @options.merge! command(name).contextualize(@relation, params)

  if command_delegated?(name)
    assoc_name = options[:delegated_commands][name.to_s]
    assoc = @relation.reflections[assoc_name].klass.scoped
    @relation.merge command(name).execute_on(assoc, params)
  else
    command(name).execute_on(@relation, params)
  end
end

Protected Instance Methods

command(type) click to toggle source
# File lib/the_grid/api.rb, line 34
def command(type)
  ::TheGrid::Api::Command.find(type)
end
command_delegated?(cmd) click to toggle source
# File lib/the_grid/api.rb, line 38
def command_delegated?(cmd)
  options[:delegated_commands] && options[:delegated_commands].has_key?(cmd.to_s)
end
configure(params) click to toggle source
# File lib/the_grid/api.rb, line 42
def configure(params)
  self.delegate(params[:delegate]) if params[:delegate]
  params.tap do |o|
    o[:cmd] = Array.wrap(o[:cmd])
    o[:cmd].unshift(:paginate) unless params[:per_page] === false
    o[:cmd].uniq!
  end
end