unpolymorph 0.1.0 → 0.1.1

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 (5) hide show
  1. data/lib/unpolymorph.rb +39 -0
  2. metadata +44 -24
  3. data/Gemfile +0 -4
  4. data/Rakefile +0 -1
  5. data/unpolymorph.gemspec +0 -19
@@ -0,0 +1,39 @@
1
+ require "unpolymorph/version"
2
+
3
+ module Unpolymorph
4
+
5
+ def unpolymorph( name, associations )
6
+
7
+ scope "for_#{name}", -> obj { where( "#{obj.class.base_class.to_s.underscore}_id" => obj.id ) }
8
+
9
+ define_method( name ) do
10
+ associations.each do |assoc|
11
+ associated = send( assoc )
12
+ return associated if associated
13
+ end
14
+ end
15
+
16
+ define_method( "#{name}=" ) do |value|
17
+ associations.each do |assoc|
18
+ if value.class.base_class.to_s.underscore == assoc.to_s
19
+ send( "#{assoc}=", value )
20
+ return
21
+ end
22
+ end
23
+ raise ArgumentError, "Cannot set #{name} to #{value}, no association matches"
24
+ end
25
+
26
+ # For use in migrations
27
+ define_singleton_method( "update_sql_#{name}_check" ) do |migration|
28
+ constraint = "exactly_one_unpolymorphic_#{name}"
29
+ migration.execute( "ALTER TABLE #{table_name} DROP CONSTRAINT IF EXISTS #{constraint}" )
30
+ migration.execute( "ALTER TABLE #{table_name} ADD CONSTRAINT #{constraint} CHECK (#{associations.collect { |a| "(#{a}_id IS NOT NULL)::int" }.join( " + ")} = 1)" )
31
+ end
32
+
33
+ define_singleton_method( "possible_#{name}?" ) do |object|
34
+ return associations.any? { |assoc| object.class.base_class.to_s.underscore == assoc.to_s }
35
+ end
36
+ end
37
+ end
38
+
39
+ ActiveRecord::Base.extend( Unpolymorph )
metadata CHANGED
@@ -1,50 +1,70 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: unpolymorph
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
4
5
  prerelease:
5
- version: 0.1.0
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Marek Janukowicz
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2013-06-06 00:00:00.000000000 Z
17
+
18
+ date: 2013-06-06 00:00:00 +02:00
19
+ default_executable:
13
20
  dependencies: []
21
+
14
22
  description: Polymorphic association replacement
15
- email:
23
+ email:
16
24
  - marek@janukowicz.net
17
25
  executables: []
26
+
18
27
  extensions: []
28
+
19
29
  extra_rdoc_files: []
20
- files:
21
- - README.md
22
- - Gemfile
23
- - unpolymorph.gemspec
24
- - Rakefile
30
+
31
+ files:
32
+ - lib/unpolymorph.rb
25
33
  - LICENSE.txt
26
- homepage: ''
34
+ - README.md
35
+ has_rdoc: true
36
+ homepage: ""
27
37
  licenses: []
38
+
28
39
  post_install_message:
29
40
  rdoc_options: []
30
- require_paths:
41
+
42
+ require_paths:
31
43
  - lib
32
- required_ruby_version: !ruby/object:Gem::Requirement
33
- requirements:
34
- - - ! '>='
35
- - !ruby/object:Gem::Version
36
- version: '0'
44
+ required_ruby_version: !ruby/object:Gem::Requirement
37
45
  none: false
38
- required_rubygems_version: !ruby/object:Gem::Requirement
39
- requirements:
40
- - - ! '>='
41
- - !ruby/object:Gem::Version
42
- version: '0'
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ hash: 3
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
54
  none: false
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ hash: 3
59
+ segments:
60
+ - 0
61
+ version: "0"
44
62
  requirements: []
63
+
45
64
  rubyforge_project:
46
- rubygems_version: 1.8.24
65
+ rubygems_version: 1.6.2
47
66
  signing_key:
48
67
  specification_version: 3
49
68
  summary: Replace your polymorphic associations with foreign key bases solution
50
69
  test_files: []
70
+
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in unpolymorph.gemspec
4
- gemspec
data/Rakefile DELETED
@@ -1 +0,0 @@
1
- require "bundler/gem_tasks"
data/unpolymorph.gemspec DELETED
@@ -1,19 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'unpolymorph/version'
5
-
6
- Gem::Specification.new do |gem|
7
- gem.name = "unpolymorph"
8
- gem.version = Unpolymorph::VERSION
9
- gem.authors = ["Marek Janukowicz"]
10
- gem.email = ["marek@janukowicz.net"]
11
- gem.description = %q{Polymorphic association replacement}
12
- gem.summary = %q{Replace your polymorphic associations with foreign key bases solution}
13
- gem.homepage = ""
14
-
15
- gem.files = Dir["**"]
16
- gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
- gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
- gem.require_paths = ["lib"]
19
- end