Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rails 4 support #9

Merged
merged 1 commit into from Sep 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 5 additions & 8 deletions lib/serialize_with_options.rb
Expand Up @@ -67,14 +67,11 @@ def method_missing(method, *args)
end

module InstanceMethods
def to_xml(*args)
set, opts = parse_serialization_options(args)
super(self.class.serialization_options(set).deep_merge(opts))
end

def as_json(*args)
set, opts = parse_serialization_options(args)
super(self.class.serialization_options(set).deep_merge(opts))
%w(to_xml to_json as_json).each do |method_name|
define_method method_name do |*args|
set, opts = parse_serialization_options(args)
super self.class.serialization_options(set).deep_merge(opts)
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion serialize_with_options.gemspec
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_dependency "activerecord", "~> 3.0"
s.add_dependency "activerecord", "~> 4.0"

s.add_development_dependency "shoulda"
s.add_development_dependency "sqlite3"
Expand Down
12 changes: 6 additions & 6 deletions test/serialize_with_options_test.rb
Expand Up @@ -78,7 +78,7 @@ class Review < ActiveRecord::Base
end
end

class SerializeWithOptionsTest < Test::Unit::TestCase
class SerializeWithOptionsTest < Minitest::Test
def self.should_serialize_with_options
should "include active_record attributes" do
assert_equal @user.name, @user_hash["name"]
Expand Down Expand Up @@ -181,15 +181,15 @@ def self.should_serialize_with_options

should "serialize the associated object properly" do
review_hash = Hash.from_xml(@review.to_xml)
assert_equal @user.email, review_hash["review"]["reviewable"]["email"]
assert_equal @user.name, review_hash["review"]["reviewable"]["name"]
end
end

context "being converted to JSON" do
setup do
@user_hash = JSON.parse(@user.to_json)["user"]
@post_hash = JSON.parse(@post.to_json)["post"]
@blog_post_hash = JSON.parse(@blog_post.to_json)["blog_post"]
@user_hash = JSON.parse(@user.to_json)
@post_hash = JSON.parse(@post.to_json)
@blog_post_hash = JSON.parse(@blog_post.to_json)
end

should_serialize_with_options
Expand All @@ -216,7 +216,7 @@ def self.should_serialize_with_options
end

should "find associations with multi-word names" do
user_hash = JSON.parse(@user.to_json(:with_check_ins))["user"]
user_hash = JSON.parse(@user.to_json(:with_check_ins))
assert_equal @check_in.code_name, user_hash['check_ins'].first['code_name']
end

Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Expand Up @@ -3,7 +3,7 @@

require 'rubygems'
require 'active_record'
require 'test/unit'
require 'minitest/autorun'
require 'shoulda'
require 'json'
require 'serialize_with_options'
Expand Down