GithubHelp home page GithubHelp logo

Comments (8)

nesaulov avatar nesaulov commented on May 3, 2024

I guess we have two ways here:

  1. Monkey-patch ActiveRecord_Relation so it has #surrealize on an instance.
  2. Force users to use Surrealist#surrealize(instance) directly.

I personally prefer second option, because of absence of monkey-patching and because ActiveRecord is not the only ORM out there, so we will have to monkey-patch a lot of things.

from surrealist.

AlessandroMinali avatar AlessandroMinali commented on May 3, 2024

Could we just map back the surrealization of each element:

    # something like this 🤷🤷 ¯\_(ツ)_/¯
    def surrealize(instance:, camelize:, include_root:)
      return instance.map { |i| surrealize(i,camelize, include_root) } if instance.respond_to?(:each) #
      ::JSON.dump(build_schema(instance: instance, camelize: camelize, include_root: include_root))
    end

I think this produces similar behaviour as ActiveModel::Serializer.

from surrealist.

nesaulov avatar nesaulov commented on May 3, 2024

@AlessandroMinali yeah, you are right, it's a third option and it looks alright. I'll think about it for a while.

from surrealist.

nesaulov avatar nesaulov commented on May 3, 2024

Actually, there is the same downside as in the first option - we'll have to monkey-patch Enumerable and else, because otherwise NoMehtodError is thrown:

User.where(id: 3).surrealize
# => NoMethodError: undefined method `surrealize' for #<User::ActiveRecord_Relation:0x007f90ae569758>

from surrealist.

AlessandroMinali avatar AlessandroMinali commented on May 3, 2024

Hmm, right.

I guess a simple solution is to introduce a new method like mentioned in the other issue which is just a wrapper to call"

return instance.map { |i| surrealize(i,camelize, include_root) } if instance.respond_to?(:each) #

Would be nice if it just worked though without a separate method.

from surrealist.

AlessandroMinali avatar AlessandroMinali commented on May 3, 2024

Here are monkey-patch attempt and a #surrealize_collection methods WIPs to compare.

Both have specs for AR, ROM, Datamapper and Sequel.

from surrealist.

nesaulov avatar nesaulov commented on May 3, 2024

thanks, @AlessandroMinali! I think #surrealize_collection way is much better than monkey-patching, so we can continue to work on that one. Am I right that the API you are planning to introduce is Surrealist.surrealize_collection(collection)?

from surrealist.

AlessandroMinali avatar AlessandroMinali commented on May 3, 2024

Yes, that's correct @nesaulov, with the ability to specific camelize and include root etc. if they so choose.

from surrealist.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.