has_extra_data 0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.tmproj
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
5
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in has_extra_data.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,48 @@
1
+ Adds an add_extra_data method to ActiveRecord that invisibly includes an extra data table. Use with STI to keep your database clean.
2
+
3
+ Uses rspec for test.
4
+
5
+ This is an example of it in use...
6
+
7
+ Usage
8
+ -------
9
+ Drink.all
10
+ Drink.first.is_hot?
11
+
12
+ Coke.all
13
+ Coke.first.is_hot?
14
+ Coke.first.is_diet?
15
+
16
+ Classes
17
+ --------
18
+
19
+ class Drink < ActiveRecord::Base
20
+ validates :is_hot, :in => [true, false]
21
+ end
22
+
23
+ class Coffee < Product
24
+ end
25
+
26
+ class Water < Product
27
+ end
28
+
29
+ class Coke < Product
30
+ has_extra_data
31
+ validates :is_diet, :in => [true, false]
32
+ end
33
+
34
+ schema
35
+
36
+ create_table :drinks do |t|
37
+ t.string type, :null => false
38
+ t.boolean :is_hot, :null => false
39
+ end
40
+
41
+ create_table :coke_data do |t|
42
+ t.integer :coke_id, :null => false # Foreign key here...
43
+ t.boolean :is_diet, :null => false
44
+ end
45
+
46
+ Todo
47
+ ----
48
+ Automatically pass attributes through.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "has_extra_data/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "has_extra_data"
7
+ s.version = HasExtraData::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jeremy Walker"]
10
+ s.email = ["jez.walker@gmail.com"]
11
+ s.homepage = ""
12
+ s.summary = %q{Ruby Rails - Add extra data to SDI models with clean database tables.}
13
+ s.description = %q{Adds an add_extra_data method to ActiveRecord that invisibly includes an extra data table. Use with STI to keep your database clean.}
14
+
15
+ s.add_dependency "rails"
16
+ s.add_development_dependency "rspec-rails"
17
+
18
+ s.rubyforge_project = "has_extra_data"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ["lib"]
24
+ end
@@ -0,0 +1,7 @@
1
+ require 'active_support/core_ext'
2
+ require File.join(File.dirname(__FILE__), "has_extra_data/hook")
3
+ require File.join(File.dirname(__FILE__), "has_extra_data/railtie")
4
+
5
+ #module HasExtraData
6
+ # autoload :Hook, File.join(File.dirname(__FILE__), "has_extra_data/hook")
7
+ #end
@@ -0,0 +1,16 @@
1
+ module HasExtraData
2
+ module Hook
3
+ def has_extra_data(&block)
4
+
5
+ table_name = "#{self.name.underscore.gsub("/", "_")}_data"
6
+ klass = Class.new(ActiveRecord::Base) do
7
+ set_table_name(table_name)
8
+ end
9
+ klass.class_eval &block if block_given?
10
+ self.const_set "Data", klass
11
+
12
+ has_one :data, :class_name => "#{self.name}::Data"
13
+ before_create :build_data
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails'
2
+ require 'has_extra_data'
3
+
4
+ module HasExtraData
5
+ class Railtie < Rails::Railtie
6
+ railtie_name :has_extra_data
7
+
8
+ config.to_prepare do
9
+ ActiveRecord::Base.send(:extend, HasExtraData::Hook)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module HasExtraData
2
+ VERSION = "0.1"
3
+ end
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe "HasExtraData" do
4
+
5
+ before :each do
6
+ if Object.const_defined?("TestClass")
7
+ Object.send(:remove_const, "TestClass")
8
+ end
9
+ @klass = Class.new(ActiveRecord::Base)
10
+ Object.const_set "TestClass", @klass
11
+ end
12
+
13
+ it "has_extra_data can be called on an AR class" do
14
+ lambda{
15
+ @klass.class_eval{has_extra_data}
16
+ }.should_not raise_error
17
+ end
18
+
19
+ it "has_extra_data adds the data method" do
20
+ @klass.class_eval{has_extra_data}
21
+ @klass.new.should respond_to(:data)
22
+ end
23
+
24
+ it "creates data with the test class" do
25
+ @klass.class_eval{has_extra_data}
26
+ obj = @klass.create!
27
+ obj.data.id.should_not == 0
28
+ end
29
+
30
+ end
@@ -0,0 +1,26 @@
1
+ ENV["RAILS_ENV"] ||= 'test'
2
+
3
+ require 'rails'
4
+ require 'rspec-rails'
5
+ require 'active_record'
6
+
7
+ $:.unshift File.dirname(__FILE__) + '/../lib'
8
+
9
+ # This isn't working so I have to use the second line...
10
+ require File.dirname(__FILE__) + '/../lib/has_extra_data'
11
+ ActiveRecord::Base.send(:extend, HasExtraData::Hook)
12
+
13
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :database => ":memory:")
14
+
15
+ # AR keeps printing annoying schema statements
16
+ $stdout = StringIO.new
17
+
18
+ ActiveRecord::Base.logger
19
+ ActiveRecord::Schema.define(:version => 1) do
20
+ create_table :test_classes do |t|
21
+ end
22
+
23
+ create_table :test_class_data do |t|
24
+ t.integer :test_class_id, :null => false
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_extra_data
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "0.1"
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Walker
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-16 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec-rails
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: Adds an add_extra_data method to ActiveRecord that invisibly includes an extra data table. Use with STI to keep your database clean.
39
+ email:
40
+ - jez.walker@gmail.com
41
+ executables: []
42
+
43
+ extensions: []
44
+
45
+ extra_rdoc_files: []
46
+
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - README
51
+ - Rakefile
52
+ - has_extra_data.gemspec
53
+ - lib/has_extra_data.rb
54
+ - lib/has_extra_data/hook.rb
55
+ - lib/has_extra_data/railtie.rb
56
+ - lib/has_extra_data/version.rb
57
+ - spec/has_extra_data.rb
58
+ - spec/spec_helper.rb
59
+ has_rdoc: true
60
+ homepage: ""
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options: []
65
+
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ requirements: []
81
+
82
+ rubyforge_project: has_extra_data
83
+ rubygems_version: 1.6.2
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Ruby Rails - Add extra data to SDI models with clean database tables.
87
+ test_files:
88
+ - spec/has_extra_data.rb
89
+ - spec/spec_helper.rb