Skip to content

Commit

Permalink
Cleanup Gemfile
Browse files Browse the repository at this point in the history
USe sqlite3 memory DB instead of tableless hack
Inherit parents definitions unless over written
  • Loading branch information
Chris Harper committed Oct 19, 2011
1 parent 7d9e8eb commit d30a034
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 85 deletions.
9 changes: 6 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
source :rubygems

gem 'rspec'
gem 'activerecord'
gem 'factory_girl'
gem 'rake'

group :test, :development do
gem 'rspec'
gem 'sqlite3'
end


52 changes: 26 additions & 26 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
GEM
remote: http://rubygems.org/
specs:
activemodel (3.0.4)
activesupport (= 3.0.4)
builder (~> 2.1.2)
i18n (~> 0.4)
activerecord (3.0.4)
activemodel (= 3.0.4)
activesupport (= 3.0.4)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activesupport (3.0.4)
arel (2.0.9)
builder (2.1.2)
diff-lcs (1.1.2)
factory_girl (1.3.3)
i18n (0.5.0)
rake (0.9.2)
rspec (2.5.0)
rspec-core (~> 2.5.0)
rspec-expectations (~> 2.5.0)
rspec-mocks (~> 2.5.0)
rspec-core (2.5.1)
rspec-expectations (2.5.0)
activemodel (3.1.1)
activesupport (= 3.1.1)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.1)
activemodel (= 3.1.1)
activesupport (= 3.1.1)
arel (~> 2.2.1)
tzinfo (~> 0.3.29)
activesupport (3.1.1)
multi_json (~> 1.0)
arel (2.2.1)
builder (3.0.0)
diff-lcs (1.1.3)
i18n (0.6.0)
multi_json (1.0.3)
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
rspec-mocks (~> 2.7.0)
rspec-core (2.7.0)
rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.5.0)
tzinfo (0.3.25)
rspec-mocks (2.7.0)
sqlite3 (1.3.4)
tzinfo (0.3.30)

PLATFORMS
ruby

DEPENDENCIES
activerecord
factory_girl
rake
rspec
sqlite3
9 changes: 6 additions & 3 deletions lib/only_expose.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ def to_xml(options = {})
module ClassMethods

def only_expose(*args)
@@exposed_attributes ||= {}
@@exposed_attributes[name] = *args.map
@exposed_attributes = *args
end

def exposed_attributes
@@exposed_attributes[name]
if @exposed_attributes.nil? && self.superclass != ActiveRecord::Base
superclass.exposed_attributes
else
@exposed_attributes
end
end
end
end
2 changes: 1 addition & 1 deletion lib/only_expose/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module OnlyExpose
VERSION = '0.0.1'
VERSION = '0.0.2'
end
4 changes: 2 additions & 2 deletions only_expose.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ Gem::Specification.new do |s|
s.name = "only_expose"
s.version = OnlyExpose::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['charper']
s.authors = ['chrisharper']
s.email = []
s.homepage = "https://github.com/charper/only_expose"
s.homepage = "https://github.com/branched/only_expose"
s.summary = "Methods to expose only specified attributes in ActiveRecord using the helpers to_json and to_xml"

s.required_rubygems_version = ">= 1.6.2"
Expand Down
30 changes: 10 additions & 20 deletions spec/only_expose_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
require 'spec_helper'

describe 'OnlyExposer :' do
describe 'OnlyExpose :' do

context 'a parent model' do

before :all do
@foo = Factory.stub :foo
end
let(:foo){Foo.new(:name => 'James' , :address => 'A Street' , :age => '18')}

it 'adds class method only_expose' do
Foo.public_methods.include? 'only_expose'
Expand All @@ -17,11 +14,11 @@
end

it 'returns only attributes defined when using to_json' do
@foo.to_json.should eq '{"foo":{"address":"A Street","name":"James"}}'
foo.to_json.should eq '{"foo":{"address":"A Street","name":"James"}}'
end

it 'returns only attributes defined when using to_xml' do
@foo.to_xml.should
foo.to_xml.should
eq '<?xml version="1.0" encoding="UTF-8"?>
<foo>
<name>James</name>
Expand All @@ -30,25 +27,18 @@
end
end

context 'a child model' do

before :all do
@bar = Factory.stub :bar
end
context 'a non overriding child model' do
let(:bar){ Bar.new(:name => 'James' , :address => 'A Street' , :age => '18')}

it 'returns all its attributes' do
@bar.to_json.should eq '{"bar":{"address":"A Different Street","age":18,"id":1002,"name":"Timmy"}}'
it 'returns all its parents defined attributes' do
bar.to_json.should eq '{"bar":{"address":"A Street","name":"James"}}'
end
end

context 'an overriding child model' do

before :all do
@baz = Factory.stub :baz
end

let(:baz){ Baz.new(:name => 'Gerald' , :address => 'A Street' , :age => '18')}
it 'returns only its specified attributes' do
@baz.to_json.should eq '{"baz":{"name":"Gerald"}}'
baz.to_json.should eq '{"baz":{"name":"Gerald"}}'
end
end

Expand Down
16 changes: 14 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
require 'only_expose'
require 'active_record'
require 'factory_girl'

load File.dirname(__FILE__) + '/support/models.rb'

ActiveRecord::Base.establish_connection(
:adapter => 'sqlite3',
:database => ':memory:'
)


class SetupTables < ActiveRecord::Migration
def change
create_table :foos do |t|
t.string :name
t.string :address
t.integer :age
end
end
end
SetupTables.new.migrate :up
28 changes: 0 additions & 28 deletions spec/support/models.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
class Foo < ActiveRecord::Base
include OnlyExpose

def self.columns() @columns ||= []; end

def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end

column :name, :string
column :address, :string
column :age, :integer

only_expose :name, :address
end

Expand All @@ -20,21 +10,3 @@ class Bar < Foo
class Baz < Foo
only_expose :name
end

Factory.define :foo do |f|
f.name 'James'
f.age 18
f.address 'A Street'
end

Factory.define :bar do |f|
f.name 'Timmy'
f.age 18
f.address 'A Different Street'
end

Factory.define :baz do |f|
f.name 'Gerald'
f.age 18
f.address 'Another City'
end

0 comments on commit d30a034

Please sign in to comment.