module Databound

Constants

VERSION

Public Instance Methods

create() click to toggle source
# File lib/databound.rb, line 21
def create
  record = @crud.create_from_data

  render json: {
    success: true,
    id: serialize(record, :id),
    scoped_records: serialize_array(scoped_records),
  }
end
destroy() click to toggle source
# File lib/databound.rb, line 41
def destroy
  @crud.destroy_from_data

  render json: {
    success: true,
    scoped_records: serialize_array(scoped_records),
  }
end
update() click to toggle source
# File lib/databound.rb, line 31
def update
  record = @crud.update_from_data

  render json: {
    success: true,
    id: serialize(record, :id),
    scoped_records: serialize_array(scoped_records),
  }
end
where() click to toggle source
# File lib/databound.rb, line 12
def where
  records = @crud.find_scoped_records

  render json: {
    success: true,
    records: serialize_array(records),
  }
end

Private Instance Methods

init_crud() click to toggle source
# File lib/databound.rb, line 71
def init_crud
  @crud = Databound::Manager.new(self)
end
scoped_records() click to toggle source
# File lib/databound.rb, line 75
def scoped_records
  records = @crud.find_scoped_records(only_extra_scopes: true)
  @crud.action_allowed?(:read, records) ? records : []
end
serialize(record, attribute) click to toggle source
# File lib/databound.rb, line 61
def serialize(record, attribute)
  unserialized = record.send(attribute)
  return unserialized.to_json unless defined?(ActiveModel::Serializer)

  serializer = ActiveModel::Serializer.serializer_for(record)
  return unserialized unless serializer

  serializer.new(record).attributes[:id]
end
serialize_array(records) click to toggle source
# File lib/databound.rb, line 52
def serialize_array(records)
  return records.to_json unless defined?(ActiveModel::Serializer)

  serializer = ActiveModel::Serializer.serializer_for(records.first)
  return records unless serializer

  ActiveModel::ArraySerializer.new(records).to_json
end