cerebris / jsonapi-resources

A resource-focused Rails library for developing JSON:API compliant servers.
http://jsonapi-resources.com
MIT License
2.32k stars 532 forks source link

Configure for use without ActiveRecord? #51

Closed realityendshere closed 8 years ago

realityendshere commented 9 years ago

In experimenting with this gem, I seem to run into trouble trying to use this with a NoSQL-style database instead of ActiveRecord. When trying to create new records with POST, I encounter the error:

uninitialized constant JSONAPI::ActiveRecordOperationsProcessor::ActiveRecord

Can the gem be configured to use alternative data storage strategies?

Thanks.

lgebhardt commented 9 years ago

@realityendshere, I'm sorry you are having trouble with a NoSQL data source. I haven't done much testing with non ActiveRecord data sources. I think the problem is that the ResourceController requires the ActiveRecordOperationsProcessor by default. I probably need to make a few changes so that isn't loaded if you are not using ActiveRecord.

I think the major change we need is to set an option for the OperationsProcessor in the configuration. Then have ResourceController.create_operations_processor use this to load the appropriate OperationsProcessor. I'll see if I can get to that soon, or you are welcome to try and fix it yourself if you want.

realityendshere commented 9 years ago

As a temporary workaround, I use the "plain" OperationsProcessor instead of the one for ActiveRecord:

#config/initializers/jsonapi_resouces.rb
require 'jsonapi/resource_controller'

module JSONAPI
  class ResourceController

    def create_operations_processor
      JSONAPI::OperationsProcessor.new
    end

  end
end

I haven't gotten too deep into if or how well this works with associations or really anything beyond rudimentary CRUD operations on a couple of records. But having a running Rails app is a start.

bvandenbos commented 9 years ago

Even with the workaround, you get this error when validations fail.

JSONAPI::Resource has ActiveRecord-based rescues in it:

    def _save
      @model.save!
      @save_needed = false
    rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved => e
      raise JSONAPI::Exceptions::ValidationErrors.new(e.record.errors.messages)
    end

https://github.com/cerebris/jsonapi-resources/blob/master/lib/jsonapi/resource.rb#L118

realityendshere commented 9 years ago

@bvandenbos This is true! See issue #127.

bvandenbos commented 9 years ago

Ah - didn't see that issue covered it already. Thanks!

acorncom commented 8 years ago

Was this handled by #192 ?

realityendshere commented 8 years ago

The projects I've worked on in the last year have all used ActiveRecord. I hesitate to confirm the fix.

But: I will close this issue. If there are still challenges using alternatives to ActiveRecord, please feel free to comment.