unpolymorph 0.1.2 → 0.2.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.
@@ -1,3 +1,3 @@
1
1
  module Unpolymorph
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/unpolymorph.rb CHANGED
@@ -2,21 +2,35 @@ require "unpolymorph/version"
2
2
 
3
3
  module Unpolymorph
4
4
 
5
- def unpolymorph( name, associations )
5
+ def unpolymorph( name, associations, options = {} )
6
6
 
7
7
  scope "for_#{name}", -> obj { where( "#{obj.class.base_class.to_s.underscore}_id" => obj.id ) }
8
8
 
9
+ if options[:create_associations]
10
+ associations.each do |assoc|
11
+ belongs_to assoc
12
+ end
13
+ end
14
+
9
15
  define_method( name ) do
16
+ var_name = "@_unpolymorph_#{name}"
17
+ value = instance_variable_get( var_name )
18
+ return value if value
10
19
  associations.each do |assoc|
11
- associated = send( assoc )
12
- return associated if associated
20
+ value = send( assoc )
21
+ if value
22
+ instance_variable_set( var_name, value )
23
+ return value
24
+ end
13
25
  end
26
+ return nil
14
27
  end
15
28
 
16
29
  define_method( "#{name}=" ) do |value|
17
30
  associations.each do |assoc|
18
31
  if value.class.base_class.to_s.underscore == assoc.to_s
19
32
  send( "#{assoc}=", value )
33
+ instance_variable_set( "@_unpolymorph_#{name}", value )
20
34
  return
21
35
  end
22
36
  end
@@ -25,9 +39,10 @@ module Unpolymorph
25
39
 
26
40
  # For use in migrations
27
41
  define_singleton_method( "update_sql_#{name}_check" ) do |migration|
28
- constraint = "exactly_one_unpolymorphic_#{name}"
42
+ required = options.has_key?( :required ) ? options[:required] : true
43
+ constraint = required ? "exactly_one_unpolymorphic_#{name}" : "at_most_one_unpolymorphic_#{name}"
29
44
  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)" )
45
+ migration.execute( "ALTER TABLE #{table_name} ADD CONSTRAINT #{constraint} CHECK (#{associations.collect { |a| "(#{a}_id IS NOT NULL)::int" }.join( " + ")} #{required ? "=" : "<="} 1)" )
31
46
  end
32
47
 
33
48
  define_singleton_method( "possible_#{name}?" ) do |object|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unpolymorph
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Marek Janukowicz
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-06-06 00:00:00 +02:00
18
+ date: 2013-06-25 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21