lib/ar_sync/rails.rb in ar_sync-1.1.0 vs lib/ar_sync/rails.rb in ar_sync-1.1.1
- old
+ new
@@ -45,12 +45,12 @@
def schema
raise 'you must implement method `schema`'
end
def sync_call
- _api_call :sync do |model, current_user, query|
- ArSync.sync_serialize model, current_user, query
+ _api_call :sync do |schema, current_user, query|
+ ArSync.sync_serialize schema, current_user, query
end
end
def graphql_schema
render plain: ArSerializer::GraphQL.definition(schema.class)
@@ -67,36 +67,33 @@
rescue StandardError => e
render json: { error: handle_exception(e) }
end
def static_call
- _api_call :static do |model, current_user, query|
- case model
- when ArSync::Collection, ActiveRecord::Relation, Array
- ArSerializer.serialize model.to_a, query, context: current_user
- when ArSerializer::Serializable
- ArSerializer.serialize model, query, context: current_user
- else
- model
- end
+ _api_call :static do |schema, current_user, query|
+ ArSerializer.serialize schema, query, context: current_user
end
end
private
def _api_call(type)
if respond_to?(ArSync.config.current_user_method)
current_user = send ArSync.config.current_user_method
end
+ sch = schema
responses = params[:requests].map do |request|
begin
api_name = request[:api]
- sch = schema
- info = sch.class._serializer_field_info api_name
- raise ArSync::ApiNotFound, "#{type.to_s.capitalize} API named `#{api_name}` not configured" unless info
- api_params = (request[:params].as_json || {}).transform_keys(&:to_sym)
- model = sch.instance_exec(current_user, **api_params, &info.data_block)
- { data: yield(model, current_user, request[:query].as_json) }
+ raise ArSync::ApiNotFound, "#{type.to_s.capitalize} API named `#{api_name}` not configured" unless sch.class._serializer_field_info api_name
+ query = {
+ api_name => {
+ as: :data,
+ params: request[:params].as_json,
+ attributes: request[:query].as_json
+ }
+ }
+ yield schema, current_user, query
rescue StandardError => e
{ error: handle_exception(e) }
end
end
render json: responses