Git Product home page Git Product logo

Comments (5)

kevinelliott avatar kevinelliott commented on May 21, 2024 1

@spohlenz Revisiting this issue that @richardvenneman had. I have jsonb in our applications too.

I was wondering what support for this officially might look like.

Would you prefer:

  1. a custom field class, json_text_area that incorporated the conversions above
  2. a flag to text_area that allowed for explicit hash/json handling but reuses existing field class
    or
  3. auto-detection of the attribute type in the model as JSON/JSONB and then internal handling of this (details still unclear where that would occur though)

?

from trestle.

spohlenz avatar spohlenz commented on May 21, 2024

There's currently a couple of ways to solve this one.

The cleanest option will be to define a custom writer method on your model:

def client_params=(params)
  params = JSON.parse(params) if params.is_a?(String)
  super(params)
end

Another option would be to define a custom params block in your admin:

params do |params|
  params.require(:booking).permit(:client_params).tap do |p|
    p[:client_params] = JSON.parse(p[:client_params])
  end
end

Eventually I'd like to add built-in support for form objects which would make this even cleaner, but this is some way off.

from trestle.

richardvenneman avatar richardvenneman commented on May 21, 2024

Thanks for the support @spohlenz. Because Rails returns the JSON as a Hash in string form I had to make a few adjustments. I created this concern for use with multiple models:

module JSONSerializable
  extend ActiveSupport::Concern

  included do
    json_attributes = attribute_names.map do |name|
      name if type_for_attribute(name).type == :jsonb
    end.compact

    json_attributes.each do |attr|
      define_method("#{attr}=".to_sym) do |value|
        value = JSON.parse(value.gsub('=>', ':')) if value.is_a?(String)
        super(value)
      end
    end
  end
end

from trestle.

spohlenz avatar spohlenz commented on May 21, 2024

The form helpers in Trestle are only responsible for the front-end rendering, not the back-end processing. Whilst they could potentially be made to handle the server-side processing too, I don't see a way of doing that without making things too "rigid".

However an approach that I have been considering since the start is to introduce a form object that could handle these sorts of things, in order to keep them from cluttering up the model.

It would probably look something like this:

form_object do
  reject_blank :tags
  json :metadata

  def title=(title)
    instance.title = title.titlecase
  end
end

from trestle.

sudoaza avatar sudoaza commented on May 21, 2024

Also don't forget the text_area :metadata, value: metadata.to_json in the form

from trestle.

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.