class Databound::Manager

Public Class Methods

new(controller) click to toggle source
# File lib/databound/manager.rb, line 6
def initialize(controller)
  @controller = controller

  @scope = Databound::Data.new(@controller, scope_js, model)
  @data = Databound::Data.new(@controller, data_js, model)

  @extra_where_scopes = JSON.parse(extra_where_scopes_js).map do |extra_scope|
    Databound::Data.new(@controller, extra_scope, model)
  end
end

Public Instance Methods

action_allowed?(method, record) click to toggle source
# File lib/databound/manager.rb, line 55
def action_allowed?(method, record)
  permit_checks = @controller.databound_config.read(:permit)
  check = permit_checks[method]
  return true unless check

  @controller.instance_exec(params, record, &check)
end
create_from_data() click to toggle source
# File lib/databound/manager.rb, line 28
def create_from_data
  check_params!(:create)
  record = model.new(params.to_h)
  check_permit!(:create, record)

  record.save
  record
end
destroy_from_data() click to toggle source
# File lib/databound/manager.rb, line 49
def destroy_from_data
  record = model.find(params.id)
  check_permit!(:destroy, record)
  record.destroy
end
find_scoped_records(only_extra_scopes: false) click to toggle source
# File lib/databound/manager.rb, line 17
def find_scoped_records(only_extra_scopes: false)
  records = or_query(@scope, *@extra_where_scopes)

  unless only_extra_scopes
    records = filter_by_params!(records)
    check_permit!(:read, records)
  end

  records
end
update_from_data() click to toggle source
# File lib/databound/manager.rb, line 37
def update_from_data
  attributes = params.to_h
  id = attributes.delete(:id)

  check_params!(:update)
  record = model.find(id)
  check_permit!(:update, record)

  record.update(attributes)
  record
end

Private Instance Methods

activerecord?() click to toggle source
# File lib/databound/manager.rb, line 138
def activerecord?
  defined?(ActiveRecord) and model.ancestors.include?(ActiveRecord::Base)
end
allowed_action_columns() click to toggle source
# File lib/databound/manager.rb, line 107
def allowed_action_columns
  @action == :update ? [:id] : []
end
bound_values(scopes) click to toggle source
# File lib/databound/manager.rb, line 75
def bound_values(scopes)
  scopes.flat_map do |scope|
    model.where(scope.to_h).bind_values
  end
end
check_params!(action) click to toggle source
# File lib/databound/manager.rb, line 81
def check_params!(action)
  @action = action
  return if columns == :all
  return if unpermitted_columns.empty?

  raise NotPermittedError, "Request includes unpermitted columns: #{unpermitted_columns.join(', ')}"
end
check_permit!(method, record) click to toggle source
# File lib/databound/manager.rb, line 89
def check_permit!(method, record)
  return if action_allowed?(method, record)

  raise NotPermittedError, "Request for #{method} not permitted"
end
columns() click to toggle source
# File lib/databound/manager.rb, line 111
def columns
  result = @controller.databound_config.read(:columns)

  case result
  when [:all]
    :all
  when [:table_columns]
    table_columns
  else
    Array(result)
  end
end
data_js() click to toggle source
# File lib/databound/manager.rb, line 156
def data_js
  @controller.params[:data]
end
extra_scope_records() click to toggle source
# File lib/databound/manager.rb, line 164
def extra_scope_records
  @extra_where_scopes.flat_map(&:records)
end
extra_where_scopes_js() click to toggle source
# File lib/databound/manager.rb, line 160
def extra_where_scopes_js
  @controller.params[:extra_where_scopes] || '[]'
end
filter_by_params!(records) click to toggle source
# File lib/databound/manager.rb, line 168
def filter_by_params!(records)
  records & or_query(params, *@extra_where_scopes)
end
model() click to toggle source
# File lib/databound/manager.rb, line 142
def model
  raise ConfigError, 'No model specified' unless model_name

  model_name.to_s.camelize.constantize
end
model_name() click to toggle source
# File lib/databound/manager.rb, line 148
def model_name
  @controller.databound_config.read(:model)
end
mongoid?() click to toggle source
# File lib/databound/manager.rb, line 134
def mongoid?
  defined?(Mongoid) and model.ancestors.include?(Mongoid::Document)
end
or_query(*scopes) click to toggle source
# File lib/databound/manager.rb, line 65
def or_query(*scopes)
  nodes = scopes.map do |scope|
    model.where(scope.to_h).where_values.reduce(:and)
  end

  model.where(nodes.reduce(:or)).tap do |q|
    q.bind_values = bound_values(scopes)
  end
end
params() click to toggle source
# File lib/databound/manager.rb, line 103
def params
  OpenStruct.new(@scope.to_h.merge(@data.to_h))
end
permit_update_destroy_block() click to toggle source
# File lib/databound/manager.rb, line 95
def permit_update_destroy_block
  @controller.class.permit_update_destroy
end
scope_js() click to toggle source
# File lib/databound/manager.rb, line 152
def scope_js
  @controller.params[:scope]
end
table_columns() click to toggle source
# File lib/databound/manager.rb, line 124
def table_columns
  if mongoid?
    model.fields.keys.map(&:to_sym)
  elsif activerecord?
    model.column_names.map(&:to_sym)
  else
    raise ConfigError, 'ORM not supported. Use ActiveRecord or Mongoid'
  end
end
unpermitted_columns() click to toggle source
# File lib/databound/manager.rb, line 99
def unpermitted_columns
  params.to_h.keys - columns - allowed_action_columns
end