tinco / nobrainer_streams

A temporary gem that implements streams in NoBrainer, hopefully it gets merged soon and this gem will be obsolete.
MIT License
8 stars 4 forks source link

deserialize changes using as_hash function if defined in query model #6

Closed eqladios closed 5 years ago

eqladios commented 5 years ago

sometimes you need to include/remove certain attributes so define as_hash function and changes shall use it rather than using the hash generated by Model.new

tinco commented 5 years ago

Hey @eqladios thanks for your pull request! Are you using nobrainer streams for something?

Did you come up with this as_hash method solution or is it defined somewhere else? If it's a new api you came up with, I think maybe it would be better to pass 'as_hash' as an option to nobrainer_stream_from, would that work for you?

eqladios commented 5 years ago

Hello @tinco, I do use it for a small project. I meant as_hash as a simple method you can define in your Model to define how your model is represented if you want. I have did such to include some associations in the changes hash instead of only rendering the hash generated by Model.new

def as_hash
  hash = {}
  hash['first_name'] = self.first_name
  hash['posts'] = Post.where(author: self.first_name) # very silly example
  return hash
end

I don't see it passed to stream_from but maybe I'm missing something

tinco commented 5 years ago

I think I'm starting to understand what you're trying to do. Have you tried renaming #as_hash to #as_json? If you're using the default callback (i.e. using ActionCable#transmit) then I think it's as_json that gets called.

I think this gem is the wrong level of abstraction to implement this feature in. It's the callbacks job to process, serialize and transmit objects, this gem just connects the source and the callback to ActionCable.

eqladios commented 5 years ago

Thank you @tinco, Yes, #as_json is actually the right/best way to do it, I was missing it and went with implementing my own method to render a jbuilder partial as I was compiling a complex JSON reply. Sorry for the very late reply :smiley:.