I`m adding one more example for the same setup:

def clear_tokens!
    self.tokens = Sequel.pg_json({})
    save(raise_on_failure: true)
end

describe '#clear_tokens!' do
    let(:user) { build(:user_with_token) }
    it 'clears all tokens for the user' do
        user.clear_session!

        expect(user.tokens).to eq({})
      end
  end

Failure/Error: expect(user.tokens).to eq({})

     

       expected: {}

            got: "{}"

     

       (compared using ==)


Hmmm.

On Monday, 22 February 2016 12:10:49 UTC+2, Stefan Slaveykov wrote:
>
> Sure. Let me first add that i`m using rails, so i also added the 
> https://github.com/TalentBox/sequel-rails gem for backwards compatibility 
> with ActiveRecord (i didn't clarify this in the beginning). So, as per the 
> gem documentation i changed a couple of things. I made some progress on 
> this and maybe the 'problem' has nothing to do with Sequel.
>
> In my config/application.rb i have:
>
> config.sequel.after_connect = proc do
>       Sequel::Model.plugin :timestamps, update_on_create: true
>       Sequel::Model.plugin :pg_typecast_on_load, :tokens
>       Sequel.extension :pg_array
>       Sequel.extension :pg_json
> end
> The docs <https://github.com/TalentBox/sequel-rails#enabling-plugins> suggest 
> that this is the right way to do it. I also tried having the plugins and 
> extensions loaded in the model, but to no good.
>
> In my app/modes/user.rb i have:
>
> class User < Sequel::Model
>   ...
>   def generate_auth_token!
>     token = SecureRandom.urlsafe_base64(nil, false)
>     token_hash = ::BCrypt::Password.create(token)
>
>     self.tokens = Sequel.pg_json({
>         token: token_hash,
>         expiry: TOKEN_EXPIRY,
>         updated_at: Time.now
>     })
>     save(raise_on_failure: true)
>   end
>
> end
>
> In db/migrate i have:
>
> Sequel.migration do
>   change do
>     create_table(:users) do
>       primary_key :id
>       ....
>       Json :tokens
>       ......
>     end
>   end
> end
>
> Given this setup i have a spec:
>
> describe '#generate_auth_token' do
>     it 'generates and authentication token' do
>       expect(subject.tokens).to be_nil
>
>       subject.generate_auth_token!
>
>       expect(subject.tokens).not_to be_a(String)
>       expect(subject.tokens).to have_key(:token) &
>                                 have_key(:expiry) &
>                                 have_key(:updated_at)
>     end
>   end
>
> The funny thing is that this spec is passing. The tokens column is 
> properly formatted. *However *when i reload the model the spec 
> fails.(this is how everything started, actually, i'm reloading the model in 
> a request spec). So if i do this:
>
> describe '#generate_auth_token' do
>     it 'generates and authentication token' do
>       expect(subject.tokens).to be_nil
>
>       subject.generate_auth_token!
>       subject.reload
>
>       expect(subject.tokens).not_to be_a(String)
>       expect(subject.tokens).to have_key(:token) &
>                                 have_key(:expiry) &
>                                 have_key(:updated_at)
>     end
>   end
>
> I get: 
>
> Failure/Error: expect(subject.tokens).not_to be_a(String)
>
>        expected 
> "{\"token\":\"$2a$10$33MwL5OuPKzhATJgEk.GiOO8T6RIWXnJfc26enzWYv3mqzzhFCRW.\",\"expiry\":\"2016-02-29T12:03:22.821+02:00\",\"updated_at\":\"2016-02-22T12:03:23.072+02:00\"}"
>  
> not to be a kind of String
>
>
> For this particular case i don`t need to reload, but in other cases i do. 
> In the end probably this has nothing to do with Sequel?
>
> On Sunday, 21 February 2016 22:34:03 UTC+2, Jeremy Evans wrote:
>>
>> On Sunday, February 21, 2016 at 12:04:57 PM UTC-8, Stefan Slaveykov wrote:
>>>
>>> I have a type json column in one of my PostgreSQL tables. Following the 
>>> docs 
>>> <http://sequel.jeremyevans.net/rdoc-plugins/files/lib/sequel/extensions/pg_json_rb.html>
>>>  i 
>>> added the pg_json extension and also loaded the pg_typecast_on_load plugin 
>>> for my model, but i'm always getting a string and not the actual json. The 
>>> code looks like this:
>>>
>>> class User < Sequel::Model
>>>   plugin :pg_typecast_on_load, :tokens
>>>
>>>   def generate_token!
>>>     self.tokens = Sequel.pg_json({
>>>       token: token_hash,
>>>       expiry: TOKEN_EXPIRY,
>>>       updated_at: Time.now
>>>     })
>>>
>>>     save(raise_on_failure: true)
>>>   end
>>> end
>>>
>>> When i do:
>>> user.tokens
>>> I get somethink like:
>>>
>>>
>>> "{\"token\":\"$2a$10$ABzWzLDx2C9Q/uLSSoErbO2CxkzKYuzpH0daR/WcD1buPV4QUBK3i\",\"expiry\":\"2016-02-28T21:25:37.789+02:00\",\"updated_at\":\"2016-02-21T21:25:38.138+02:00\"}"
>>>
>>>
>>> I`m pretty new to Sequel( i find it really fun and easy to work with) 
>>> and maybe i missed something along the way, but i just don`t know what 
>>> exactly. I tried using strings, instead of symbols for keys, removing the 
>>> pg_typecase_on_load plugin, but nothing helped. What am i doing wrong? 
>>> Should be something pretty straightforward.
>>>
>>
>> Assuming you did DB.extension :pg_json before creating the model class, 
>> things should just work and you should not need to use the 
>> pg_typecast_on_load plugin.  Based on your description, I'm not sure why 
>> you are having the problem. The only thing I can think of would be using 
>> Sequel.extension :pg_json instead of DB.extension :pg_json, but considering 
>> you linked to the correct documentation and the documentation shows 
>> DB.extension :pg_json, that seems unlikely.
>>
>> Can you post a self contained example showing the problem?
>>
>> 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 https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to