estepnv / leafy Goto Github PK
View Code? Open in Web Editor NEWToolkit for custom attributes in Ruby apps
License: MIT License
Toolkit for custom attributes in Ruby apps
License: MIT License
Any reason I'd be seeing this in my rails 6 app?
NameError: uninitialized constant #<Class:0x00007ff1e9c351f8>::Leafy
My Model has this:
class TeamContact < ApplicationRecord
include Leafy::Mixin::Schema[:active_record]
...
end
We use Leafy with ActiveRecord and Postgres. Our leafy_data
type is jsonb. leafy_fields
and leafy_values
work fine.
But sometimes we need to use data from leafy_data
field directly in SQL queries. Because of serialization to JSON text before saving to the database Postgres thinks it is a string and can't parse it or convert to JSON type. So the problem is that we can't use leafy_data
in SQL queries.
I think, one of the possible solutions is to modify FieldValueCollection
dump
and load
to not use json serialization for ActiveRecord json/jsonb type fields. What do you think about this?
Leafy::FieldValueCollection
def self.dump(field_values_collection, json_serialization = true)
collection = field_values_collection
.map { |field_value| [field_value.id, field_value.raw] }
.to_h
json_serialization ? JSON.dump(collection) : collection
end
def self.load(leafy_fields, data, json_serialization = true)
Leafy::FieldValueCollection.new(
leafy_fields,
json_serialization ? JSON.load(data) : data
)
end
And call it like that:
Leafy::Mixin::ActiveRecord::Fields::InstanceMethods
def leafy_field_values
:Leafy::FieldValueCollection.load(leafy_fields, leafy_data || "{}", activerecord_json_column?)
end
Using Rails 6, ActiveRecord, and Postgres
After retrieving an existing model from the database, I updated the model to have a "custom" field:
tc.update(
leafy_fields_attributes: [
{ name: "hs_city", type: :string}
]
)
Although, upon trying to pull the values (which I'd expect to be nil) I get an undefined method 'leafy_values'
error. So no getter...
So I thought perhaps I had to assign values (even though that shouldn't be the case)
tc.leafy_values ={ "hs_city": 'Gallatin'}
But this threw basically the same error undefined method 'leafy_values='
so no setter...
But indeed the object did store the "custom" field:
tc.leafy_fields
=> #<Leafy::Schema:0x00007fcc0875f400 @fields=[#<Leafy::Field:0x00007fcc0875f3b0 @id="hs_city-eed709f9-fea3-47ad-97e1-5e81ef800a66", @metadata={}, @name="hs_city", @type="string">]>
Am I missing something from the Read me? Or perhaps another initializer/config?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.