Skip to content

Commit

Permalink
Getting started: attributes.
Browse files Browse the repository at this point in the history
Super super basic collection of attributes. Nothing fancy.
  • Loading branch information
steveklabnik committed Jul 9, 2014
1 parent a45b5ee commit 729a823
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 12 deletions.
1 change: 1 addition & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require "bundler/gem_tasks"
require 'rake/testtask'

Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/*_test.rb']
t.ruby_opts = ['-r./test/test_helper.rb']
t.verbose = true
Expand Down
4 changes: 2 additions & 2 deletions active_model_serializers.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'active_model_serializers/version'
require 'active_model/serializer/version'

Gem::Specification.new do |spec|
spec.name = "active_model_serializers"
spec.version = ActiveModelSerializers::VERSION
spec.version = ActiveModel::Serializer::VERSION

This comment has been minimized.

Copy link
@bf4

bf4 Dec 23, 2015

Member

@steveklabnik Do you remember why you made these changes? At this point, Given the history of AMS naming with respect to Rails, it would seem ActiveModelSerializers to be a safer namespace than ActiveModel::Serializer, especially since in 0.10 the adapter and serializer work together to render a model serialization.

-require 'active_model_serializers/version'
 +require 'active_model/serializer/version'

  Gem::Specification.new do |spec|
    spec.name          = "active_model_serializers"
 -  spec.version       = ActiveModelSerializers::VERSION
 +  spec.version       = ActiveModel::Serializer::VERSION

and in 4a2d985

-# ActiveModelSerializers
+# ActiveModel::Serializers 
spec.authors = ["Steve Klabnik"]
spec.email = ["[email protected]"]
spec.summary = %q{Conventions-based JSON generation for Rails.}
Expand Down
21 changes: 21 additions & 0 deletions lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module ActiveModel
class Serializer
class << self
attr_accessor :_attributes
end

def self.inherited(base)
base._attributes = []
end

def self.attributes(*attrs)
@_attributes.concat attrs
end

attr_accessor :object

def initialize(object)
@object = object
end
end
end
5 changes: 5 additions & 0 deletions lib/active_model/serializer/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module ActiveModel
class Serializer
VERSION = "0.9.0"
end
end
8 changes: 4 additions & 4 deletions lib/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require "active_model_serializers/version"
require "active_model"
require "active_model/serializer/version"

require "active_model/serializer"

module ActiveModelSerializers
# Your code goes here...
end
3 changes: 0 additions & 3 deletions lib/active_model_serializers/version.rb

This file was deleted.

18 changes: 18 additions & 0 deletions test/attributes_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'test_helper'

module ActiveModel
class Serializer
class AttributesTest < Minitest::Test
def setup
@profile = Profile.new({ name: 'Name 1', description: 'Description 1', comments: 'Comments 1' })
@profile_serializer = ProfileSerializer.new(@profile)
end

def test_attributes_definition
assert_equal([:name, :description],
@profile_serializer.class._attributes)
end
end
end
end

12 changes: 12 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Model
def initialize(hash={})
@attributes = hash
end
end

class Profile < Model
end

class ProfileSerializer < ActiveModel::Serializer
attributes :name, :description
end
7 changes: 4 additions & 3 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
require "bundler/setup"

require "active_model_serializers"
require 'rails'
require "active_support/json"
require 'minitest/autorun'

require 'rails'
require "active_model_serializers"

require 'minitest/autorun'
require 'fixtures/poro'

0 comments on commit 729a823

Please sign in to comment.