Comments (5)
@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:
- a custom field class,
json_text_area
that incorporated the conversions above - a flag to
text_area
that allowed for explicit hash/json handling but reuses existing field class
or - 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.
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.
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.
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.
Also don't forget the text_area :metadata, value: metadata.to_json
in the form
from trestle.
Related Issues (20)
- how show unchecked checkbox in a column(show table)? HOT 1
- Implementing rescue_from HOT 2
- adding maps HOT 3
- Saving seems to not provide a notice of saving but saves anyways HOT 1
- select empty if no relation? HOT 2
- How to add icons in sidebar buttons? HOT 4
- How to fix "to_param called on non-resourceful admin. You may need to explicitly specify the admin." HOT 1
- Listing dates per page HOT 1
- uninitialized constant custom field rails 7 HOT 3
- Changing date format HOT 3
- Option to disable form as "show" view? HOT 1
- My custom helper not is recognized HOT 2
- How i use concerns at trestle? HOT 1
- Remove Multiple Records From MODEL Page
- WebSockets / action_cable support? HOT 2
- Pagination for a custom index View
- Fix typo for Vietnamese locale HOT 1
- Direct upload or chunked file upload support? HOT 2
- Fails without sassc-rails
- Unable to locate favicon.ico in public folder HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from trestle.