activerecord-chemistry 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +10 -0
  3. data/.rspec +1 -0
  4. data/.ruby-version +1 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +4 -0
  7. data/Gemfile.lock +95 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +42 -0
  10. data/Rakefile +2 -0
  11. data/activerecord-chemistry.gemspec +34 -0
  12. data/bin/console +14 -0
  13. data/bin/setup +8 -0
  14. data/circle.yml +3 -0
  15. data/lib/activerecord/chemistry/actable/instance_methods.rb +36 -0
  16. data/lib/activerecord/chemistry/actable/migration.rb +27 -0
  17. data/lib/activerecord/chemistry/actable/relation.rb +39 -0
  18. data/lib/activerecord/chemistry/actable.rb +11 -0
  19. data/lib/activerecord/chemistry/extendable/instance_methods.rb +36 -0
  20. data/lib/activerecord/chemistry/extendable/migration.rb +27 -0
  21. data/lib/activerecord/chemistry/extendable/relation.rb +36 -0
  22. data/lib/activerecord/chemistry/extendable.rb +11 -0
  23. data/lib/activerecord/chemistry/representable/class_methods.rb +8 -0
  24. data/lib/activerecord/chemistry/representable/instance_methods.rb +49 -0
  25. data/lib/activerecord/chemistry/representable/migration.rb +27 -0
  26. data/lib/activerecord/chemistry/representable/relation.rb +49 -0
  27. data/lib/activerecord/chemistry/representable.rb +11 -0
  28. data/lib/activerecord/chemistry/version.rb +5 -0
  29. data/lib/activerecord/chemistry.rb +30 -0
  30. data/spec/actable/actable_spec.rb +5 -0
  31. data/spec/actable/acts_as_spec.rb +63 -0
  32. data/spec/database_helper.rb +26 -0
  33. data/spec/extendable/extendable_by_spec.rb +5 -0
  34. data/spec/extendable/extends_spec.rb +63 -0
  35. data/spec/factories.rb +22 -0
  36. data/spec/migrations_spec.rb +128 -0
  37. data/spec/models.rb +56 -0
  38. data/spec/representable/representable_spec.rb +5 -0
  39. data/spec/representable/represents_spec.rb +91 -0
  40. data/spec/spec_helper.rb +35 -0
  41. metadata +223 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 28286abd968e52e11339892d89633afdf8ecfabf
4
+ data.tar.gz: bc470a17de74b6026d458a335c92bd92bcb03a29
5
+ SHA512:
6
+ metadata.gz: 42d8e0687dfd55185099ada2b552b32cdb7015530ae032e234fddaf18218af4abdc4a554c2f28e8b85033c5cc326be695dd8def003098d8036274b87fa6cb624
7
+ data.tar.gz: f00f843c8d4bd6314c3cc70a8922ffb0832d6b588431215845e75405e9cc708a3d8286708e67f90fe1779fa901f9f6cd97b8ce16c9530bb71dc169b239a3ba45
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at thom@thombruce.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in inheritance.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ activerecord-chemistry (0.1.0)
5
+ activerecord (>= 5.1)
6
+ activesupport (>= 5.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.1.3)
12
+ activesupport (= 5.1.3)
13
+ activerecord (5.1.3)
14
+ activemodel (= 5.1.3)
15
+ activesupport (= 5.1.3)
16
+ arel (~> 8.0)
17
+ activesupport (5.1.3)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (~> 0.7)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ arel (8.0.0)
23
+ ast (2.3.0)
24
+ concurrent-ruby (1.0.5)
25
+ coveralls (0.8.21)
26
+ json (>= 1.8, < 3)
27
+ simplecov (~> 0.14.1)
28
+ term-ansicolor (~> 1.3)
29
+ thor (~> 0.19.4)
30
+ tins (~> 1.6)
31
+ diff-lcs (1.3)
32
+ docile (1.1.5)
33
+ factory_girl (4.8.0)
34
+ activesupport (>= 3.0.0)
35
+ i18n (0.8.6)
36
+ json (2.1.0)
37
+ minitest (5.10.3)
38
+ parallel (1.12.0)
39
+ parser (2.4.0.0)
40
+ ast (~> 2.2)
41
+ powerpack (0.1.1)
42
+ rainbow (2.2.2)
43
+ rake
44
+ rake (12.0.0)
45
+ rspec (3.6.0)
46
+ rspec-core (~> 3.6.0)
47
+ rspec-expectations (~> 3.6.0)
48
+ rspec-mocks (~> 3.6.0)
49
+ rspec-core (3.6.0)
50
+ rspec-support (~> 3.6.0)
51
+ rspec-expectations (3.6.0)
52
+ diff-lcs (>= 1.2.0, < 2.0)
53
+ rspec-support (~> 3.6.0)
54
+ rspec-mocks (3.6.0)
55
+ diff-lcs (>= 1.2.0, < 2.0)
56
+ rspec-support (~> 3.6.0)
57
+ rspec-support (3.6.0)
58
+ rubocop (0.49.1)
59
+ parallel (~> 1.10)
60
+ parser (>= 2.3.3.1, < 3.0)
61
+ powerpack (~> 0.1)
62
+ rainbow (>= 1.99.1, < 3.0)
63
+ ruby-progressbar (~> 1.7)
64
+ unicode-display_width (~> 1.0, >= 1.0.1)
65
+ ruby-progressbar (1.8.1)
66
+ simplecov (0.14.1)
67
+ docile (~> 1.1.0)
68
+ json (>= 1.8, < 3)
69
+ simplecov-html (~> 0.10.0)
70
+ simplecov-html (0.10.1)
71
+ sqlite3 (1.3.13)
72
+ term-ansicolor (1.6.0)
73
+ tins (~> 1.0)
74
+ thor (0.19.4)
75
+ thread_safe (0.3.6)
76
+ tins (1.15.0)
77
+ tzinfo (1.2.3)
78
+ thread_safe (~> 0.1)
79
+ unicode-display_width (1.3.0)
80
+
81
+ PLATFORMS
82
+ ruby
83
+
84
+ DEPENDENCIES
85
+ activerecord-chemistry!
86
+ bundler (~> 1.14)
87
+ coveralls (~> 0.8)
88
+ factory_girl (~> 4.8.0)
89
+ rake (~> 12.0)
90
+ rspec (~> 3)
91
+ rubocop (~> 0.49.1)
92
+ sqlite3 (~> 1.3)
93
+
94
+ BUNDLED WITH
95
+ 1.14.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Thom Bruce
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # ActiveRecord::Chemistry
2
+
3
+ [![CircleCI](https://img.shields.io/circleci/project/github/thombruce/activerecord-chemistry.svg)](https://circleci.com/gh/thombruce/activerecord-chemistry) [![Coveralls](https://img.shields.io/coveralls/thombruce/activerecord-chemistry.svg)](https://coveralls.io/github/thombruce/activerecord-chemistry) [![Gemnasium](https://img.shields.io/gemnasium/thombruce/activerecord-chemistry.svg)](https://gemnasium.com/github.com/thombruce/activerecord-chemistry) [![Code Climate](https://img.shields.io/codeclimate/github/thombruce/activerecord-chemistry.svg)](https://codeclimate.com/github/thombruce/activerecord-chemistry) [![Beerpay](https://beerpay.io/thombruce/activerecord-chemistry/badge.svg?style=beer)](https://beerpay.io/thombruce/activerecord-chemistry)
4
+
5
+
6
+ Based on and inspired by [ActiveRecord::ActsAs](https://github.com/krautcomputing/active_record-acts_as). For simple multiple table inheritance, use that. ActiveRecord::Chemistry adds more complex relationships and aims to enforce referential integrity.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'activerecord-chemistry'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install activerecord-chemistry
23
+
24
+ ## Usage
25
+
26
+ TODO: Write usage instructions here
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/thombruce/activerecord-chemistry. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
37
+
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
42
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,34 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord/chemistry/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord-chemistry"
8
+ spec.version = ActiveRecord::Chemistry::VERSION
9
+ spec.authors = ["Thom Bruce"]
10
+ spec.email = ["thom@thombruce.com"]
11
+
12
+ spec.summary = %q{Adds inheritance logic for Rails ActiveRecord.}
13
+ spec.description = %q{Adds several different inheritance patterns for use with Rails models and ActiveRecord relations.}
14
+ spec.homepage = "http://github.com/thombruce/activerecord-chemistry"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.required_ruby_version = ">= 2.4"
23
+
24
+ spec.add_development_dependency "sqlite3", "~> 1.3"
25
+ spec.add_development_dependency "bundler", "~> 1.14"
26
+ spec.add_development_dependency "coveralls", "~> 0.8"
27
+ spec.add_development_dependency "factory_girl", "~> 4.8.0"
28
+ spec.add_development_dependency "rake", "~> 12.0"
29
+ spec.add_development_dependency "rspec", "~> 3"
30
+ spec.add_development_dependency "rubocop", "~> 0.49.1"
31
+
32
+ spec.add_dependency "activesupport", ">= 5.1"
33
+ spec.add_dependency "activerecord", ">= 5.1"
34
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "inheritance"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ database:
2
+ override:
3
+ - echo "Skipping database."
@@ -0,0 +1,36 @@
1
+ module ActiveRecord
2
+ module Chemistry
3
+ module Actable
4
+ module InstanceMethods
5
+ def actable_must_be_valid
6
+ unless acting_as.valid?
7
+ acting_as.errors.each do |attribute, message|
8
+ errors.add(attribute, message) unless errors[attribute].include?(message)
9
+ end
10
+ end
11
+ end
12
+ protected :actable_must_be_valid
13
+
14
+ def respond_to?(name, include_private = false, as_original_class = false)
15
+ if as_original_class
16
+ super(name, include_private)
17
+ else
18
+ super(name, include_private) || acting_as.respond_to?(name)
19
+ end
20
+ end
21
+
22
+ def self_respond_to?(name, include_private = false)
23
+ respond_to? name, include_private, true
24
+ end
25
+
26
+ def method_missing(method, *args, &block)
27
+ if !self_respond_to?(method) && acting_as.respond_to?(method)
28
+ acting_as.send(method, *args, &block)
29
+ else
30
+ super
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ module ActiveRecord
2
+ module Chemistry
3
+ module Actable
4
+ module Migration
5
+ module TableDefinition
6
+ def acts_as(*args)
7
+ options = args.extract_options!
8
+ args.each do |col|
9
+ references(col, options)
10
+ end
11
+ end
12
+ end
13
+
14
+ module Table
15
+ include TableDefinition
16
+
17
+ def remove_acts_as(*args)
18
+ options = args.extract_options!
19
+ args.each do |col|
20
+ @base.remove_reference(@name, col, options)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,39 @@
1
+ require 'activerecord/chemistry/actable/instance_methods'
2
+
3
+ module ActiveRecord
4
+ module Chemistry
5
+ module Actable
6
+ module Relation
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ end
11
+
12
+ class_methods do
13
+ def acts_as(name, scope = nil, options = {})
14
+ if Hash === scope
15
+ options = scope
16
+ scope = nil
17
+ end
18
+ options[:dependent] ||= :destroy
19
+ reflections = belongs_to(name, scope, options)
20
+ default_scope -> { includes(name) }
21
+ validate :actable_must_be_valid
22
+
23
+ cattr_reader(:acting_as_reflection) { reflections.stringify_keys[name.to_s] }
24
+ cattr_reader(:acting_as_model) { (options[:class_name] || name.to_s.camelize).constantize }
25
+ class_eval "def #{name}; super || build_#{name}(acting_as_model.actable_reflection.name => self); end"
26
+ alias_method :acting_as, name
27
+ alias_method :acting_as=, "#{name}=".to_sym
28
+ include Actable::InstanceMethods
29
+ end
30
+
31
+ def actable(name, scope = nil, options = {})
32
+ reflections = has_one(name, scope, options.merge(dependent: :destroy))
33
+ cattr_reader(:actable_reflection) { reflections.stringify_keys[name.to_s] }
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,11 @@
1
+ require 'activerecord/chemistry/actable/migration'
2
+ require 'activerecord/chemistry/actable/relation'
3
+
4
+ module ActiveRecord
5
+ module Chemistry
6
+ module Actable
7
+ include ActiveRecord::Chemistry::Actable::Migration
8
+ include ActiveRecord::Chemistry::Actable::Relation
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module ActiveRecord
2
+ module Chemistry
3
+ module Extendable
4
+ module InstanceMethods
5
+ def extendable_must_be_valid
6
+ unless extendable.valid?
7
+ extendable.errors.each do |attribute, message|
8
+ errors.add(attribute, message) unless errors[attribute].include?(message)
9
+ end
10
+ end
11
+ end
12
+ protected :extendable_must_be_valid
13
+
14
+ def respond_to?(name, include_private = false, as_original_class = false)
15
+ if as_original_class
16
+ super(name, include_private)
17
+ else
18
+ super(name, include_private) || extendable.respond_to?(name)
19
+ end
20
+ end
21
+
22
+ def self_respond_to?(name, include_private = false)
23
+ respond_to? name, include_private, true
24
+ end
25
+
26
+ def method_missing(method, *args, &block)
27
+ if !self_respond_to?(method) && extendable.respond_to?(method)
28
+ extendable.send(method, *args, &block)
29
+ else
30
+ super
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,27 @@
1
+ module ActiveRecord
2
+ module Chemistry
3
+ module Extendable
4
+ module Migration
5
+ module TableDefinition
6
+ def extended_by(*args)
7
+ options = args.extract_options!
8
+ args.each do |col|
9
+ references(col, options)
10
+ end
11
+ end
12
+ end
13
+
14
+ module Table
15
+ include TableDefinition
16
+
17
+ def remove_extended_by(*args)
18
+ options = args.extract_options!
19
+ args.each do |col|
20
+ @base.remove_reference(@name, col, options)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,36 @@
1
+ require 'activerecord/chemistry/extendable/instance_methods'
2
+
3
+ module ActiveRecord
4
+ module Chemistry
5
+ module Extendable
6
+ module Relation
7
+ extend ActiveSupport::Concern
8
+
9
+ module ClassMethods
10
+ def extendable_by(name, scope = nil, options = {})
11
+ reflections = belongs_to(name, scope, options.merge(dependent: :destroy, optional: true))
12
+ cattr_reader(:extended_by_reflection) { reflections.stringify_keys[name.to_s] }
13
+ end
14
+
15
+ def extends(name, scope = nil, options = {})
16
+ if Hash === scope
17
+ options = scope
18
+ scope = nil
19
+ end
20
+ options[:dependent] ||= :nullify
21
+ reflections = has_one(name, scope, options)
22
+ default_scope -> { includes(name) }
23
+ validate :extendable_must_be_valid
24
+
25
+ cattr_reader(:extendable_reflection) { reflections.stringify_keys[name.to_s] }
26
+ cattr_reader(:extendable_model) { (options[:class_name] || name.to_s.camelize).constantize }
27
+ class_eval "def #{name}; super || build_#{name}(extendable_model.extended_by_reflection.name => self); end"
28
+ alias_method :extendable, name
29
+ alias_method :extendable=, "#{name}=".to_sym
30
+ include Extendable::InstanceMethods
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ require 'activerecord/chemistry/extendable/migration'
2
+ require 'activerecord/chemistry/extendable/relation'
3
+
4
+ module ActiveRecord
5
+ module Chemistry
6
+ module Extendable
7
+ include ActiveRecord::Chemistry::Extendable::Migration
8
+ include ActiveRecord::Chemistry::Extendable::Relation
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module ActiveRecord
2
+ module Chemistry
3
+ module Representable
4
+ module ClassMethods
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,49 @@
1
+ module ActiveRecord
2
+ module Chemistry
3
+ module Representable
4
+ module InstanceMethods
5
+ def representable_must_be_valid
6
+ unless representing.valid?
7
+ representing.errors.each do |attribute, message|
8
+ errors.add(attribute, message) unless errors[attribute].include?(message)
9
+ end
10
+ end
11
+ end
12
+ protected :representable_must_be_valid
13
+
14
+ def find_by_or_keep_representable
15
+ if representing.new_record? && @findable = representing_model.find_by(self.representing.attributes.reject { |k| ['id', 'created_at', 'updated_at'].include? k })
16
+ self.representing = @findable
17
+ #@findable.association(representing_model.representable_reflection.name.to_sym).add_to_target(self, true)
18
+ end
19
+ end
20
+
21
+ def update_or_instantiate_representable
22
+ if representing.changed? && representing.send(representing_model.representable_reflection.name.to_sym).count > 1
23
+ self.representing = representing.dup
24
+ end
25
+ end
26
+
27
+ def respond_to?(name, include_private = false, as_original_class = false)
28
+ if as_original_class
29
+ super(name, include_private)
30
+ else
31
+ super(name, include_private) || representing.respond_to?(name)
32
+ end
33
+ end
34
+
35
+ def self_respond_to?(name, include_private = false)
36
+ respond_to? name, include_private, true
37
+ end
38
+
39
+ def method_missing(method, *args, &block)
40
+ if !self_respond_to?(method) && representing.respond_to?(method)
41
+ representing.send(method, *args, &block)
42
+ else
43
+ super
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,27 @@
1
+ module ActiveRecord
2
+ module Chemistry
3
+ module Representable
4
+ module Migration
5
+ module TableDefinition
6
+ def represents(*args)
7
+ options = args.extract_options!
8
+ args.each do |col|
9
+ references(col, options)
10
+ end
11
+ end
12
+ end
13
+
14
+ module Table
15
+ include TableDefinition
16
+
17
+ def remove_represents(*args)
18
+ options = args.extract_options!
19
+ args.each do |col|
20
+ @base.remove_reference(@name, col, options)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end