Code Monkey home page Code Monkey logo

leafy's People

Contributors

estepnv avatar sqlninja avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

shushpan sqlninja

leafy's Issues

Uninitialized constant

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

Avoid dumping and parsing JSON for jsonb field

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

Unable to update values

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?

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.