fixjour 0.1.6 → 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
data/lib/core_ext/hash.rb CHANGED
@@ -6,4 +6,4 @@ class Hash
6
6
  replace(indifferent)
7
7
  merge!(keys_values)
8
8
  end
9
- end
9
+ end
@@ -3,4 +3,4 @@ class Object
3
3
  yield self
4
4
  self
5
5
  end
6
- end
6
+ end
@@ -6,7 +6,7 @@ module Fixjour
6
6
  Generator.new(builder.klass, block).call(self, args.extract_options!.symbolize_keys!)
7
7
  end
8
8
  end
9
-
9
+
10
10
  # Defines the create_* method
11
11
  def define_create(builder)
12
12
  define_method("create_#{builder.name}") do |*args|
@@ -15,16 +15,40 @@ module Fixjour
15
15
  model
16
16
  end
17
17
  end
18
-
18
+
19
19
  # Defines the valid_*_attributes method
20
20
  def define_valid_attributes(builder)
21
21
  define_method("valid_#{builder.name}_attributes") do |*args|
22
- valid_attributes = send("new_#{builder.name}", *args).attributes
22
+ record = send("new_#{builder.name}", *args)
23
+ valid_attributes = record.attributes
23
24
  valid_attributes.delete_if { |key, value| value.nil? }
25
+
26
+ transfer_singular_ids = proc { |reflection|
27
+ if associated = record.send(reflection.name)
28
+ associated.new_record? && associated.save!
29
+ key = reflection.options[:foreign_key] || reflection.name.to_s + '_id'
30
+ valid_attributes[key] = associated.id
31
+ end
32
+ }
33
+
34
+ record.class.reflect_on_all_associations(:has_one).each(&transfer_singular_ids)
35
+ record.class.reflect_on_all_associations(:belongs_to).each(&transfer_singular_ids)
36
+
37
+ transfer_plural_ids = proc { |reflection|
38
+ if associated = record.send(reflection.name)
39
+ associated.each { |rec| rec.new_record? && rec.save! }
40
+ key = reflection.options[:foreign_key] || reflection.name.to_s + '_id'
41
+ valid_attributes[key.to_s + 's'] = associated.map(&:id)
42
+ valid_attributes.delete(reflection.name)
43
+ end
44
+ }
45
+
46
+ record.class.reflect_on_all_associations(:has_many).each(&transfer_plural_ids)
47
+
24
48
  valid_attributes.stringify_keys!
25
49
  valid_attributes.make_indifferent!
26
50
  valid_attributes
27
51
  end
28
52
  end
29
53
  end
30
- end
54
+ end
@@ -21,4 +21,4 @@ module Fixjour
21
21
  end
22
22
  end
23
23
 
24
- Fixjour::MergingProxy.send :include, Fixjour::Deprecation::MergingProxy
24
+ Fixjour::MergingProxy.send :include, Fixjour::Deprecation::MergingProxy
@@ -1,33 +1,33 @@
1
1
  module Fixjour
2
2
  # Raised when a builder returns an invalid object.
3
3
  class InvalidBuilder < StandardError; end
4
-
4
+
5
5
  # Raised when a builder will return an invalid object
6
6
  # due to a validates_uniqueness_of validation.
7
7
  class DangerousBuilder < StandardError; end
8
-
8
+
9
9
  # Raised when a builder returns an object that cannot
10
10
  # be saved the database.
11
11
  class UnsavableBuilder < StandardError; end
12
-
12
+
13
13
  # Raised when a builder returns an object of the wrong type
14
14
  class WrongBuilderType < StandardError; end
15
-
15
+
16
16
  # Raised when a builder block saves the object.
17
17
  class BuilderSavedRecord < StandardError; end
18
-
18
+
19
19
  # Raised when a Fixjour creation method is called in
20
20
  # the wrong context.
21
21
  class NonBlockBuilderReference < StandardError; end
22
-
22
+
23
23
  # Raised when a builder is defined for a class that already
24
24
  # has one.
25
25
  class RedundantBuilder < StandardError; end
26
-
26
+
27
27
  # Raised when a builder is defined with one block argument and
28
28
  # the user assumes that it's the overrides hash. This used to
29
29
  # be the standard behavior, but now blocks with one argument
30
30
  # are passed the class proxy, and getting access to the overrides
31
31
  # hash requires you pass two block arguments.
32
32
  class DeprecatedMergeAttempt < StandardError; end
33
- end
33
+ end
@@ -1,13 +1,13 @@
1
1
  module Fixjour
2
- # This generates a new instance of a model object for
2
+ # This generates a new instance of a model object for
3
3
  # the new_[model] method.
4
4
  class Generator
5
5
  attr_reader :klass, :block
6
-
6
+
7
7
  def initialize(klass, block)
8
8
  @klass, @block = klass, block
9
9
  end
10
-
10
+
11
11
  def call(context, overrides={})
12
12
  overrides = OverridesHash.new(overrides)
13
13
  result = block.bind(context).call(*args(overrides))
@@ -16,7 +16,7 @@ module Fixjour
16
16
  else result
17
17
  end
18
18
  end
19
-
19
+
20
20
  def args(overrides)
21
21
  case block.arity
22
22
  when 1 then [MergingProxy.new(klass, overrides)]
@@ -24,4 +24,4 @@ module Fixjour
24
24
  end
25
25
  end
26
26
  end
27
- end
27
+ end
@@ -4,36 +4,36 @@ module Fixjour
4
4
  # the #new method is called.
5
5
  class MergingProxy
6
6
  instance_methods.each { |m| undef_method(m) unless m =~ /__|inspect/ }
7
-
7
+
8
8
  def initialize(klass, overrides)
9
9
  @klass = klass
10
10
  @overrides = overrides
11
11
  end
12
-
12
+
13
13
  def protected(*attrs)
14
14
  attrs = attrs.empty? ?
15
15
  @protected :
16
16
  @protected = attrs
17
17
  Set.new(attrs)
18
18
  end
19
-
19
+
20
20
  def new(defaults={})
21
21
  attrs = defaults.merge(@overrides)
22
22
  accessible, inaccessible = partition(attrs)
23
-
23
+
24
24
  returning @klass.new(accessible) do |instance|
25
25
  inaccessible.each do |key,val|
26
26
  instance.send("#{key}=", val)
27
27
  end
28
28
  end
29
29
  end
30
-
30
+
31
31
  def method_missing(sym, *args, &block)
32
32
  @klass.respond_to?(sym) ? @klass.send(sym, *args, &block) : super
33
33
  end
34
-
34
+
35
35
  private
36
-
36
+
37
37
  def partition(attrs)
38
38
  accessible = attrs.keys.inject({ }) do |m, key|
39
39
  next m if protected.include?(key)
@@ -43,4 +43,4 @@ module Fixjour
43
43
  [accessible, attrs]
44
44
  end
45
45
  end
46
- end
46
+ end
@@ -4,11 +4,11 @@ module Fixjour
4
4
  # method private and add the ability to process.
5
5
  class OverridesHash < Hash
6
6
  private :delete
7
-
7
+
8
8
  def initialize(hash)
9
9
  replace(hash)
10
10
  end
11
-
11
+
12
12
  # Allows for processing of the overrides hash. Deletes
13
13
  # the option when it's present, then yields the value.
14
14
  def process(option)
@@ -17,4 +17,4 @@ module Fixjour
17
17
  end
18
18
  end
19
19
  end
20
- end
20
+ end
data/lib/fixjour.rb CHANGED
@@ -1,6 +1,5 @@
1
1
  $LOAD_PATH << File.dirname(__FILE__)
2
2
 
3
- require 'rubygems'
4
3
  require 'activerecord'
5
4
  require 'core_ext/hash'
6
5
  require 'core_ext/object'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fixjour
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima
@@ -22,6 +22,26 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: faker
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: acts_as_fu
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
25
45
  description:
26
46
  email: patnakajima@gmail.com
27
47
  executables: []
@@ -31,10 +51,8 @@ extensions: []
31
51
  extra_rdoc_files: []
32
52
 
33
53
  files:
34
- - lib/core_ext
35
54
  - lib/core_ext/hash.rb
36
55
  - lib/core_ext/object.rb
37
- - lib/fixjour
38
56
  - lib/fixjour/builder.rb
39
57
  - lib/fixjour/builders.rb
40
58
  - lib/fixjour/counter.rb
@@ -49,6 +67,8 @@ files:
49
67
  - lib/fixjour.rb
50
68
  has_rdoc: true
51
69
  homepage: http://github.com/nakajima/fixjour
70
+ licenses: []
71
+
52
72
  post_install_message:
53
73
  rdoc_options: []
54
74
 
@@ -69,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
89
  requirements: []
70
90
 
71
91
  rubyforge_project:
72
- rubygems_version: 1.3.1
92
+ rubygems_version: 1.3.3
73
93
  signing_key:
74
94
  specification_version: 2
75
95
  summary: Object creation methods everyone already has