From 20a7aff56deae270e0191f3aacea0cbc22e62854 Mon Sep 17 00:00:00 2001 From: Jester Date: Tue, 14 Aug 2018 01:17:08 +0300 Subject: [PATCH] Fix Render --- Gemfile.lock | 26 +++++++++++----------- lib/frontend_api/render/base_render.rb | 12 +++++----- lib/frontend_api/render/command_render.rb | 1 - lib/frontend_api/render/messages_render.rb | 6 ++++- lib/frontend_api/resources/resource.rb | 20 ++++++++++++----- 5 files changed, 37 insertions(+), 28 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 41b4b9f..33a543c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -24,23 +24,23 @@ GEM rack (2.0.5) rack-protection (2.0.3) rack - rack-test (1.0.0) + rack-test (1.1.0) rack (>= 1.0, < 3) rake (12.3.1) - rspec (3.7.0) - rspec-core (~> 3.7.0) - rspec-expectations (~> 3.7.0) - rspec-mocks (~> 3.7.0) - rspec-core (3.7.1) - rspec-support (~> 3.7.0) - rspec-expectations (3.7.0) + rspec (3.8.0) + rspec-core (~> 3.8.0) + rspec-expectations (~> 3.8.0) + rspec-mocks (~> 3.8.0) + rspec-core (3.8.0) + rspec-support (~> 3.8.0) + rspec-expectations (3.8.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-mocks (3.7.0) + rspec-support (~> 3.8.0) + rspec-mocks (3.8.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-support (3.7.1) - sequel (5.9.0) + rspec-support (~> 3.8.0) + rspec-support (3.8.0) + sequel (5.11.0) sinatra (2.0.3) mustermann (~> 1.0) rack (~> 2.0) diff --git a/lib/frontend_api/render/base_render.rb b/lib/frontend_api/render/base_render.rb index 2197bc7..a5b1029 100644 --- a/lib/frontend_api/render/base_render.rb +++ b/lib/frontend_api/render/base_render.rb @@ -9,7 +9,9 @@ def self.render_json(parameters) end def find!(klass) - klass[params[klass.primary_key]] or halt 404 + result = klass[params[klass.primary_key]] + return result if result + defined?(halt) ? halt(404) : raise(NestedConstFinder.nested_const_get([self.class], 'NotFoundError')) end def render_resources(resources) @@ -17,9 +19,9 @@ def render_resources(resources) model_name = resource.name.demodulize.chomp('Resource') filter_name = "#{model_name.classify.pluralize}Filter" filter = begin - NestedConstFinder.nested_const_get(Module.nesting, filter_name) + NestedConstFinder.nested_const_get([resource.parent], filter_name) rescue NameError - NestedConstFinder.nested_const_get(Module.nesting, 'DatasetFilter') + NestedConstFinder.nested_const_get([resource.parent], 'DatasetFilter') end h[model_name.tableize] = { attributes: resource.attributes, @@ -31,10 +33,6 @@ def render_resources(resources) end render json: resources end - - def render_not_found - render status: :not_found, json: { messages: [format_error('Object not found')] } - end end end end diff --git a/lib/frontend_api/render/command_render.rb b/lib/frontend_api/render/command_render.rb index d9eacf2..0047878 100644 --- a/lib/frontend_api/render/command_render.rb +++ b/lib/frontend_api/render/command_render.rb @@ -30,7 +30,6 @@ def render_success(object, success = true, warnings: [], status: 200) success = action_message if success.is_a?(TrueClass) messages = format_messages([], warnings) messages << { type: :success, text: success } if success.is_a?(String) - render status: status, json: serialize_object(object, meta: { messages: messages }) end diff --git a/lib/frontend_api/render/messages_render.rb b/lib/frontend_api/render/messages_render.rb index 2d8769c..ede85e0 100644 --- a/lib/frontend_api/render/messages_render.rb +++ b/lib/frontend_api/render/messages_render.rb @@ -38,7 +38,11 @@ def schedule_message end def action_message - send :"#{request.request_method.downcase}_message" if respond_to?(:"#{request.request_method.downcase}_message") + if defined?(action_name) + send :"#{action_name}_message" if respond_to?(:"#{action_name}_message") + else + send :"#{request.request_method.downcase}_message" if respond_to?(:"#{request.request_method.downcase}_message") + end end def entity_name diff --git a/lib/frontend_api/resources/resource.rb b/lib/frontend_api/resources/resource.rb index 020fb07..cc5af5f 100644 --- a/lib/frontend_api/resources/resource.rb +++ b/lib/frontend_api/resources/resource.rb @@ -46,9 +46,18 @@ def attribute(name, options={}) # DSL method def association(name, options={}) options = options.merge(@scope) + assoc = model_class.association_reflection(name) - define_method(name) do - @object.send(name) + if block_given? + define_method(name) do + val = yield(@object.send("#{name.to_s}_dataset")) + val = val.first unless assoc.returns_array? + val + end + else + define_method(name) do + @object.send(name) + end end @associations ||= {} @@ -69,7 +78,6 @@ def association(name, options={}) opts[:readable] = !options[:writeonly] if options.key?(:writeonly) end - assoc = model_class.association_reflection(name) klass = Object.const_get(assoc[:class_name]) @associations[name] = @associations[name].merge( class: klass, @@ -78,7 +86,7 @@ def association(name, options={}) if assoc.returns_array? @associations[name][:type] = :many @associations[name][:attribute] = attr = :"#{name.to_s.singularize}_pks" - attribute attr, options.merge(type: :integer, array: true) + attribute attr, options.merge(type: :integer, array: true, writeonly: true) else @associations[name][:type] = :one @associations[name][:attribute] = attr = assoc[:key] @@ -153,9 +161,9 @@ def find_attributes(conditions) end end - def resource_for(name, nesting = Module.nesting) + def resource_for(name, resource_nesting = Module.nesting) name = "#{name}Resource" - NestedConstFinder.nested_const_get(nesting, name) + NestedConstFinder.nested_const_get(resource_nesting, name) rescue NameError raise MissingResource, "Missing resource #{name}" end