object_attorney 1.1.2 → 1.2.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.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MDM2ZTM1ZmE4MmYzMmMxYjdhMDdiNjY3Y2IzZWM2YzY4MjE0MDM3Yw==
4
+ ZDBmMmQ5OTNkMjI2MzRkZGVkNjM5NzgwMDg1NzQ0ZjYzNjhiODUwMQ==
5
5
  data.tar.gz: !binary |-
6
- NGJjOGY0OGRjMzYzNzZjMTVhZDExMzliMTAzN2I5NzlkMmZmZTBiMg==
6
+ YmQxOGY0OGNiMzEyNDQzOTcxOGM3MGRmZjhiOTVlYTdiOTdlNGVmMw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OTQyOWQ5OWMxMWY5MWQ0ZWJiOTM2MGFjYTJmY2U1MWZiODE3ZmQwYjJkN2My
10
- YzA0ODY1N2U5ZGMzMTYyNTYwYWJhZjQ0YmU3NThiNjBmMmQ0ODQ2ZGUyMDkw
11
- OTA4Y2MzNDU5NjVjZjFmYzhjNjIwZTNkYWQxNzI4ZWU5MDY5Y2Q=
9
+ ZTU2YjYyYzQ4M2JkZDJjZTZlZTg1N2JjMjNmM2JkZjhmMzczNWRiZjQ1MTBk
10
+ YzZkYzBiNDg2ZjlhMTU5ZGNhZDIzNWJmNDI4MTQzZWFiYWFjYmQzZjBhZWRl
11
+ ZjZlYWY1MDk4ZmVlZThkMGE1YTE5OTg1ODVlOTZmMDJjNjIxNjc=
12
12
  data.tar.gz: !binary |-
13
- NTgzNTc1OTUzYWM5ZDc0YmEwMWI2NDgxZTE3ODE4NTE5OGM5YzAwZDllNzQ2
14
- ODgzYzNjNTc4OGM0YTc1NmViOGNjNWE1NGNlNzU3MjQwNDkwMWQyOTc0NDhl
15
- YWNhNGY5NmVmYzkxOTRjYTc2ZTJiOGI1OGQ4NmRmMmM0ZTJiYzk=
13
+ NzY5MmJjMTMxMWU0NzVlYzZlZDYwNzM3YzNkM2Y0MzY2ZWIxYWU3MTUwNWYx
14
+ NWUxNzE3NWUzNWMzZjc4YjI5YjQ3YTM1Y2Q1YWQ2YWZjZDllOGVjM2YzODMw
15
+ YzRkYTA0ODI4NTMwZjRkZTU5MWM3MDJkYWQxYWNlYzA0ZmFiOTY=
@@ -0,0 +1,33 @@
1
+ module ObjectAttorney
2
+
3
+ class AssociationReflection
4
+ attr_reader :klass, :macro, :options, :name
5
+
6
+ def initialize(association, options)
7
+ options = options.is_a?(Hash) ? options : { class_name: options }
8
+
9
+ @name = association
10
+ @macro = options[:macro] || macro_default(association)
11
+ @klass = options[:class_name] || klass_default(association)
12
+ @options = options
13
+
14
+ @klass = @klass.constantize if @klass.is_a?(String)
15
+ end
16
+
17
+ private ################################# private
18
+
19
+ def macro_default(association)
20
+ Helpers.plural?(association) ? :has_many : :belongs_to
21
+ end
22
+
23
+ def klass_default(association)
24
+ if Helpers.plural?(association)
25
+ association.to_s.singularize.camelize
26
+ else
27
+ association.to_s.camelize
28
+ end
29
+ end
30
+
31
+ end
32
+
33
+ end
@@ -0,0 +1,19 @@
1
+ module ObjectAttorney
2
+
3
+ module Helpers
4
+
5
+ extend self
6
+
7
+ def plural?(string)
8
+ string = string.to_s
9
+ string == string.pluralize
10
+ end
11
+
12
+ def try_or_return(object, method, default_value)
13
+ returning_value = object.try(method)
14
+ returning_value.nil? ? default_value : returning_value
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -116,28 +116,29 @@ module ObjectAttorney
116
116
 
117
117
  module ClassMethods
118
118
 
119
- def accepts_nested_objects(*nested_objects_list)
120
- self.instance_variable_set("@nested_objects", nested_objects_list)
121
- self.send(:attr_accessor, *nested_objects_list.map { |attribute| "#{attribute}_attributes".to_sym })
122
- define_nested_objects_getter_methods nested_objects_list
119
+ def accepts_nested_objects(nested_object_name, options = {})
120
+ reflection = AssociationReflection.new(nested_object_name, options)
121
+ self.instance_variable_set("@#{nested_object_name}_reflection", reflection)
122
+ self.instance_variable_set("@association_reflections", association_reflections | [reflection])
123
+
124
+ self.send(:attr_accessor, "#{nested_object_name}_attributes".to_sym)
125
+ define_method(nested_object_name) { nested_getter(nested_object_name) }
123
126
  end
124
127
 
125
- def reflect_on_association(association)
126
- nil
128
+ def association_reflections
129
+ self.instance_variable_get("@association_reflections") || zuper_method('association_reflections') || []
127
130
  end
128
131
 
129
- def nested_objects
130
- self.instance_variable_get("@nested_objects") || zuper_method('nested_objects') || []
132
+ def reflect_on_association(association)
133
+ self.instance_variable_get("@#{association}_reflection") || zuper_method('reflect_on_association', association)
131
134
  end
132
135
 
133
- private #################### PRIVATE METHODS DOWN BELOW ######################
136
+ def reflect_on_all_associations(macro = nil)
137
+ macro ? association_reflections.select { |reflection| reflection.macro == macro } : association_reflections
138
+ end
134
139
 
135
- def define_nested_objects_getter_methods(nested_objects_list)
136
- nested_objects_list.each do |nested_object_name|
137
- define_method(nested_object_name) do
138
- nested_getter(nested_object_name)
139
- end
140
- end
140
+ def nested_objects
141
+ association_reflections.map(&:name)
141
142
  end
142
143
 
143
144
  end
@@ -6,11 +6,11 @@ module ObjectAttorney
6
6
  end
7
7
 
8
8
  def new_record?
9
- try_or_return(represented_object, :new_record?, true)
9
+ Helpers.try_or_return(represented_object, :new_record?, true)
10
10
  end
11
11
 
12
12
  def persisted?
13
- try_or_return(represented_object, :persisted?, false)
13
+ Helpers.try_or_return(represented_object, :persisted?, false)
14
14
  end
15
15
 
16
16
  def save
@@ -1,3 +1,3 @@
1
1
  module ObjectAttorney
2
- VERSION = "1.1.2"
2
+ VERSION = "1.2.1"
3
3
  end
@@ -1,4 +1,6 @@
1
1
  require "object_attorney/version"
2
+ require "object_attorney/helpers"
3
+ require "object_attorney/association_reflection"
2
4
  require "object_attorney/nested_objects"
3
5
  require "object_attorney/orm"
4
6
  require 'active_record'
@@ -44,7 +46,7 @@ module ObjectAttorney
44
46
  end
45
47
 
46
48
  def validate_represented_object
47
- valid = override_validations? ? true : try_or_return(represented_object, :valid?, true)
49
+ valid = override_validations? ? true : Helpers.try_or_return(represented_object, :valid?, true)
48
50
  import_represented_object_errors unless valid
49
51
  valid
50
52
  end
@@ -81,23 +83,20 @@ module ObjectAttorney
81
83
  marked_for_destruction?
82
84
  end
83
85
 
84
- def try_or_return(object, method, default_value)
85
- returning_value = object.try(method)
86
- returning_value.nil? ? default_value : returning_value
87
- end
88
-
89
86
  module ClassMethods
90
87
 
91
- def represents(represented_object_name, represented_object_class = nil)
92
- self.instance_variable_set("@represented_object_class", represented_object_class || represented_object_name.to_s.camelize.constantize)
88
+ def represents(represented_object_name, options = {})
89
+ self.instance_variable_set("@represented_object_reflection", AssociationReflection.new(represented_object_name, options))
93
90
 
94
- define_method(represented_object_name) do
95
- represented_object
96
- end
91
+ define_method(represented_object_name) { represented_object }
92
+ end
93
+
94
+ def represented_object_reflection
95
+ self.instance_variable_get("@represented_object_reflection") || zuper_method('represented_object_reflection')
97
96
  end
98
97
 
99
98
  def represented_object_class
100
- self.instance_variable_get("@represented_object_class") || zuper_method('represented_object_class')
99
+ represented_object_reflection.try(:klass)
101
100
  end
102
101
 
103
102
  def zuper_method(method_name, *args)
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "activerecord"
23
24
  end
@@ -2,7 +2,7 @@ require "spec_helper"
2
2
 
3
3
  describe BulkPostsFormChild do
4
4
 
5
- it "Creating multiple Posts, with a tabless model 'BulkPostsFormChild' has if it had 'accepts_nested_attributes_for :posts'" do
5
+ it "Creating multiple Posts, with a tabless model 'BulkPostsFormChild' has if it had 'accepts_nested_attributes_for :posts'", current: true do
6
6
  params = {
7
7
  bulk_post: {
8
8
  admin: true,
@@ -49,7 +49,7 @@ describe BulkPostsFormChild do
49
49
  }
50
50
  }
51
51
  }
52
-
52
+
53
53
  existing_post = Post.create(title: "My title1")
54
54
  BulkPostsFormChild.new(params[:bulk_post]).save
55
55
  existing_post.reload
data/spec/spec_helper.rb CHANGED
@@ -13,13 +13,15 @@ require 'object_attorney'
13
13
  require 'support/database_setup'
14
14
  require 'support/active_model/validations'
15
15
  require 'support/models/post'
16
+ require 'support/models/item'
16
17
  require 'support/models/post_form'
17
18
  require 'support/models/post_form_child'
18
19
  require 'support/models/bulk_posts_form'
19
20
  require 'support/models/bulk_posts_form_child'
20
21
 
21
22
  RSpec.configure do |config|
22
-
23
+ config.filter_run :current
24
+
23
25
  I18n.enforce_available_locales = false
24
26
 
25
27
  # see: http://iain.nl/testing-activerecord-in-isolation
@@ -2,6 +2,18 @@ class BulkPostsFormChild < BulkPostsForm
2
2
 
3
3
  attr_accessor :admin
4
4
 
5
+ accepts_nested_objects :items
6
+
5
7
  validates_presence_of :admin
6
8
 
9
+ ##################### BODY BELLOW THIS LINE ####################
10
+
11
+ def build_item(attributes = {}, item = nil)
12
+ Item.new(attributes, item)
13
+ end
14
+
15
+ def existing_items
16
+ []
17
+ end
18
+
7
19
  end
@@ -0,0 +1,3 @@
1
+ class Item
2
+ # fake model
3
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: object_attorney
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - João Gonçalves
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-13 00:00:00.000000000 Z
11
+ date: 2013-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ! '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Form Object pattern implementation for Rails
42
56
  email:
43
57
  - goncalves.joao@gmail.com
@@ -56,6 +70,8 @@ files:
56
70
  - db/migrate/20131205114900_create_posts.rb
57
71
  - db/schema.rb
58
72
  - lib/object_attorney.rb
73
+ - lib/object_attorney/association_reflection.rb
74
+ - lib/object_attorney/helpers.rb
59
75
  - lib/object_attorney/nested_objects.rb
60
76
  - lib/object_attorney/nested_uniqueness_validator.rb
61
77
  - lib/object_attorney/orm.rb
@@ -71,6 +87,7 @@ files:
71
87
  - spec/support/database_setup.rb
72
88
  - spec/support/models/bulk_posts_form.rb
73
89
  - spec/support/models/bulk_posts_form_child.rb
90
+ - spec/support/models/item.rb
74
91
  - spec/support/models/post.rb
75
92
  - spec/support/models/post_form.rb
76
93
  - spec/support/models/post_form_child.rb
@@ -110,6 +127,7 @@ test_files:
110
127
  - spec/support/database_setup.rb
111
128
  - spec/support/models/bulk_posts_form.rb
112
129
  - spec/support/models/bulk_posts_form_child.rb
130
+ - spec/support/models/item.rb
113
131
  - spec/support/models/post.rb
114
132
  - spec/support/models/post_form.rb
115
133
  - spec/support/models/post_form_child.rb