On Thursday, November 12, 2015 at 11:41:38 PM UTC-8, Matt G wrote:
>
> Hey Jeremy,
>
> Am using the JSON Serializer plugin.
>
> Am defining the includes on a Transaction Model, like this:
>
> plugin :json_serializer, :include=> [ :messages, :transfers, :user ]
>
> I make the query in the controller and merge that with some other data in
> a hash and then call:
>
> JSON(data)
>
> To format my response.
> This formats the data for me, and my joining models are included.
>
>
> I have another expensive controller method which I want don't want to
> include any of the joining models, and only some of the data.
>
> The below wont work because I have created a hash around the model.
> plugin :json_serializer
>
> data.to_json(:include=> [ :messages, :transfers, :user ])
> data.to_json(:only=> [ :lat, :lng])
>
>
> To get around this I am thinking of using:
> Sequel::Plugins::JsonSerializer.configure(Sequel::Model::Transaction,
> {:include=>[], only: [:lat, :lng]})
>
> To override the configuration, and then resetting it afterwards.
>
>
> Is there a nice way to do this, or am I going to have to rethink how I am
> using the JSON serializer plugin?
>
Currently, there's not a nice way to do this, other than:
def model_object.to_json
super(:include=> [ :messages, :transfers, :user ])
end
before using the model later.
I've thought about this use case before, and the best way I can think to
implement it is to have a json_serializer_opts accessor for model
instances. If you call the setter, it will save the custom options, and
calling to_json on the model object later will merge those options into the
default options. Anyone want to implement that? If not, I'll try to get it
in before the next release.
Thanks,
Jeremy
--
You received this message because you are subscribed to the Google Groups
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.