./lib/lux/api/lib/model_api.rb in lux-fw-0.1.17 vs ./lib/lux/api/lib/model_api.rb in lux-fw-0.1.35

- old
+ new

@@ -54,16 +54,16 @@ can? :create, @object @object.save if @object.valid? return if report_errros_if_any @object if @object.id - @message = "#{@class_name.capitalize} created" + message '%s created' % @class_name.capitalize else error 'object not created, error unknown' end - @response[:path] = (@object.path rescue nil) + response.meta :path, @object.path if @object.respond_to?(:path) @object.attributes end def update @@ -74,31 +74,35 @@ end can? :update, @object @object.save if @object.valid? report_errros_if_any @object - @message = "#{@class_name} updated" - @response[:path] = (@object.path rescue nil) + response.message '%s updated' % @class_name + response.meta :path, @object.path if @object.respond_to?(:path) @object.attributes end # if you put active boolean field to objects, then they will be unactivated on destroy def destroy error "Object not found" unless @object can? :delete, @object if @object.respond_to?(:is_active) @object.update is_active: false - @message = 'Object deleted (exists in trashcan)' + + message 'Object deleted (exists in trashcan)' elsif @object.respond_to?(:active) @object.update active: false - @message = 'Object deleted (exists in trashcan)' + + message 'Object deleted (exists in trashcan)' else @object.destroy - @message = "#{@object.class.name} deleted" + message '%s deleted' % @object.class.name end + report_errros_if_any @object + @object.attributes end def undelete error "Object not found" unless @object @@ -110,29 +114,27 @@ @object.update :active=>true else error "No is_active, can't undelete" end - @message = 'Object raised from the dead.' + response.message = 'Object raised from the dead.' end - def report_errros_if_any(obj) - @response[:object] = obj.class.to_s.tableize.singularize - @response[:objects] = obj.class.to_s.tableize - + def report_errros_if_any obj if obj.errors.count > 0 - @response[:error] = '' - @response[:errors] = {} - for k,v in obj.errors + for k, v in obj.errors desc = v.join(', ') - desc = "#{k} #{desc}" if desc.starts_with?('is ') - @response[:errors][k] = desc - @response[:error] += ', ' unless @response[:error].blank? - @response[:error] += desc + + response.error k, desc + response.error desc + # desc = "#{k} #{desc}" if desc.starts_with?('is ') end - Lux.page.status(400) + + Lux.page.status 400 + return true end + false end def can?(action, object) ModelPolicy.can?(action: action, model: object, user: Lux.page.var.user) do |err|