Skip to content

Commit

Permalink
Use proper serialization and default to AMS when available.
Browse files Browse the repository at this point in the history
  • Loading branch information
nybblr committed Mar 28, 2014
1 parent 489664d commit 754064f
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 9 deletions.
17 changes: 14 additions & 3 deletions lib/robin/rails.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
require 'active_support/concern'
# require 'robin'
require 'robin/rails/default_serializer'

module Robin
module Rails
extend ActiveSupport::Concern

included do
after_create do |record|
::Robin.publish record, :created, record.attributes
::Robin.publish record, :created, robin_serializer(record).as_json
end

after_update do |record|
::Robin.publish record, :updated, record.attributes
::Robin.publish record, :updated, robin_serializer(record).as_json
end

after_destroy do |record|
::Robin.publish record, :destroyed, id: record.id
end
end

private

def robin_serializer(resource)
serializer =
(resource.respond_to?(:active_model_serializer) &&
resource.active_model_serializer) ||
::Robin::Rails::DefaultSerializer

serializer.new(resource)
end

module ClassMethods
end
end
Expand Down
21 changes: 21 additions & 0 deletions lib/robin/rails/default_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Robin
module Rails
class DefaultSerializer
attr_reader :resource
attr_reader :options

def initialize(resource, options={})
@resource = resource
@options = options
end

def as_json
resource.as_json(options)
end

def to_json
resource.to_json(options)
end
end
end
end
3 changes: 2 additions & 1 deletion robin-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "bundler", "~> 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rails"
spec.add_development_dependency "rails", ">= 4.0.0"
spec.add_development_dependency "rspec"
spec.add_development_dependency "guard"
spec.add_development_dependency "guard-rspec"
spec.add_development_dependency "active_model_serializers", "~> 0.8.1"
end
13 changes: 13 additions & 0 deletions spec/fixtures/ams_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
require 'active_model_serializers'

class AmsModel < ActiveRecord::Base
include Robin::Rails
end

class AmsModelSerializer < ActiveModel::Serializer
attributes :name, :age, :boom

def boom
"BOOM!"
end
end
34 changes: 29 additions & 5 deletions spec/robin/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'active_record'

require_relative '../fixtures/dummy_model'
require_relative '../fixtures/ams_model'

describe Robin::Rails do
before(:all) do
Expand All @@ -14,15 +15,19 @@

ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define do
create_table :dummy_models, force: true
create_table :dummy_models, force: true do |t|
t.string "name"
t.integer "age"
end

create_table :ams_models, force: true do |t|
t.string "name"
t.integer "age"
end
end
end

context 'included module' do
before do
Robin.stub(:publish)
end

it 'calls publish after create' do
Robin.should_receive(:publish).with(anything, :created, anything)

Expand All @@ -41,4 +46,23 @@
record.destroy
end
end

context 'alternate serializers' do
let(:default_json) { {"name"=>"ams", "age"=>5} }
let(:ams_json) { {:ams_model=>{:name=>"ams", :age=>5, :boom=>"BOOM!"}} }

it 'uses default serializer' do
Robin.should_receive(:publish)
.with(anything, :created, hash_including("id", default_json))

DummyModel.create :name => "ams", :age => 5
end

it 'uses AMS when available' do
Robin.should_receive(:publish)
.with(anything, :created, ams_json)

AmsModel.create :name => "ams", :age => 5
end
end
end

0 comments on commit 754064f

Please sign in to comment.