Allow looking up channel by data attributes (nested json values)

This commit is contained in:
David Taylor 2017-07-13 22:50:14 +01:00
parent e850fb194b
commit ab2e4c2de8
2 changed files with 14 additions and 1 deletions

View File

@ -45,4 +45,6 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel
scope :with_provider, ->(provider) { where("value::json->>'provider'=?", provider)}
scope :with_data_value, ->(key, value) { where("(value::json->>'data')::json->>?=?", key, value)}
end

View File

@ -32,13 +32,24 @@ RSpec.describe DiscourseChat::Channel do
expect(DiscourseChat::Channel.with_provider('dummy').length).to eq(1)
end
it 'can be filtered by data value' do
channel2 = DiscourseChat::Channel.create!(provider:'dummy2', data:{val:"foo"})
channel3 = DiscourseChat::Channel.create!(provider:'dummy2', data:{val:"blah"})
expect(DiscourseChat::Channel.all.length).to eq(2)
for_provider = DiscourseChat::Channel.with_provider('dummy2')
expect(for_provider.length).to eq(2)
expect(DiscourseChat::Channel.with_provider('dummy2').with_data_value('val','blah').length).to eq(1)
end
it 'can find its own rules' do
channel = DiscourseChat::Channel.create({provider:'dummy'})
expect(channel.rules.size).to eq(0)
DiscourseChat::Rule.create(channel: channel)
DiscourseChat::Rule.create(channel: channel)
expect(channel.rules.size).to eq(2)
end
describe 'validations' do