linker 0.0.12 → 0.0.13

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 211f9391f1bbcb2d22be5a502350da538c78d832
4
- data.tar.gz: eaf03f9335c5fda1328ab368cf63261f5997bf50
3
+ metadata.gz: 6f9eb0fbcfcc447101d0f027e48f4629797cd3ac
4
+ data.tar.gz: efbb3242eaf1badba9f5fc47ec5f9292917fa8c8
5
5
  SHA512:
6
- metadata.gz: 0bdb09982452a1638328e2f1f07e1a28fbda1208767d4efe1b8dad04981c5e95e55a06acecccb145c4a20e74556f4f048feabad251bbdd69a74be2be55f2610c
7
- data.tar.gz: 6efa622c9b1ea0f6d10530946de2d09f049b6acd02c8121c928ba69fec7e9199e87f518d44c2ad55f242c17a9946bc2d224ef8fc72c75db2169ec8bb1b359bce
6
+ metadata.gz: 2c46308f866502dcde44718902c531cc87507e318dfa7d3ee04ce87e657f1b9b56730d504b822de2cb14d401361df387f9dae22b25ce48d3ac4dd7662fa4f1a5
7
+ data.tar.gz: 2684c6ed01fa6b29c0046305c2e958442d2f0524c4ea0d97e8280016b9ba5c8fc7d767160ecca4c3c7705abd450183f6a951037d8f361e56f3f6b35d63e3c135
data/.travis.yml CHANGED
@@ -1,14 +1,31 @@
1
1
  language: ruby
2
+
2
3
  rvm:
3
4
  - 2.0.0
4
5
  - 2.1.0
5
6
  - 2.2.0
7
+ - 2.3.0
8
+
6
9
  env:
7
10
  - "RAILS_VERSION=3.1.0"
8
11
  - "RAILS_VERSION=3.2.0"
9
12
  - "RAILS_VERSION=4.0.0"
10
13
  - "RAILS_VERSION=4.1.0"
11
14
  - "RAILS_VERSION=4.2.0"
12
- - "RAILS_VERSION=master"
15
+
16
+ matrix:
17
+ include:
18
+ # Rails 5.x only supports 2.2+
19
+ - rvm: 2.3.0
20
+ env: RAILS_VERSION=master
21
+ - rvm: 2.3.0
22
+ env: RAILS_VERSION=5.0.0
23
+ - rvm: 2.2.2
24
+ env: RAILS_VERSION=5.0.0
25
+
26
+ allow_failures:
27
+ - rvm: 2.3.0
28
+ env: RAILS_VERSION=master
29
+
13
30
  notifications:
14
- email: false
31
+ email: false
data/Gemfile CHANGED
@@ -9,9 +9,9 @@ rails = case rails_version
9
9
  when "master"
10
10
  {github: "rails/rails"}
11
11
  when "default"
12
- "~> 4.1.5"
12
+ "~> 5.0.0"
13
13
  else
14
14
  "~> #{rails_version}"
15
15
  end
16
16
 
17
- gem "rails", rails
17
+ gem "rails", rails
@@ -23,19 +23,20 @@ module Linker
23
23
  end
24
24
 
25
25
  def set_reader_for_main_model
26
- #ap "criando reader for main model #{@main_model.to_s.underscore}"
27
26
  # Create attr reader for main model
28
27
  self.class.__send__(:attr_reader, @main_model.to_s.underscore)
29
- end
28
+ end
30
29
 
31
30
  def set_delegations
32
31
  # Delegate fields for main model
33
- filter_columns(@main_model).each{|c| delegate_attr(c, @main_model.to_s.underscore) }
32
+ filter_columns(@main_model).each do |c|
33
+ delegate_attr(c, @main_model.to_s.underscore)
34
+ end
34
35
 
35
36
  [map_has_one_associations, map_belongs_to_associations].each do |rgroup|
36
37
  rgroup.each do |c|
37
38
  c[:columns].each do |cc|
38
- #ap "delegando #{cc} e #{cc}= para #{c[:name]}__#{cc}"
39
+ # ap "delegating #{cc} and #{cc}= for #{c[:name]}__#{cc}"
39
40
  self.class.__send__(:delegate, cc, "#{cc}=", to: c[:name].underscore.to_sym, prefix: "#{c[:name]}_")
40
41
  end
41
42
  end
@@ -43,104 +44,105 @@ module Linker
43
44
  end
44
45
 
45
46
  private
46
- def get_has_many_associations
47
- @hm_assoc ||= @main_model.reflect_on_all_associations(:has_many)
48
- end
49
47
 
50
- def get_has_one_associations
51
- @ho_assoc ||= @main_model.reflect_on_all_associations(:has_one)
52
- end
48
+ def get_has_many_associations
49
+ @hm_assoc ||= @main_model.reflect_on_all_associations(:has_many)
50
+ end
53
51
 
54
- def get_belongs_to_associations
55
- @bt_assoc ||= @main_model.reflect_on_all_associations(:belongs_to)
56
- end
52
+ def get_has_one_associations
53
+ @ho_assoc ||= @main_model.reflect_on_all_associations(:has_one)
54
+ end
57
55
 
58
- def filter_columns model
59
- f = model.columns.map(&:name)
60
- .delete_if{|cn| USELESS_COLUMNS_REGEX.match(cn) }
61
- # Get Paperclip attachments
62
- begin
63
- f = Paperclip::AttachmentRegistry.names_for(model).inject(f) do |t, c|
64
- t << c.to_s
65
- end
66
- rescue
56
+ def get_belongs_to_associations
57
+ @bt_assoc ||= @main_model.reflect_on_all_associations(:belongs_to)
58
+ end
59
+
60
+ def filter_columns(model)
61
+ f = model.columns.map(&:name)
62
+ .delete_if{ |cn| USELESS_COLUMNS_REGEX.match(cn) }
63
+ # Get Paperclip attachments
64
+ begin
65
+ f = Paperclip::AttachmentRegistry.names_for(model).inject(f) do |t, c|
66
+ t << c.to_s
67
67
  end
68
- f
68
+ rescue
69
69
  end
70
+ f
71
+ end
70
72
 
71
- def delegate_attr att, class_to
72
- #ap "delegando #{att} e #{att}= para #{class_to.underscore.pluralize.to_sym}"
73
- self.class.__send__(:delegate, att, "#{att}=", to: class_to.underscore.to_sym)
74
- end
73
+ def delegate_attr(att, class_to)
74
+ # ap "delegating #{att} and #{att}= for #{class_to.underscore.pluralize.to_sym}"
75
+ self.class.__send__(:delegate, att, "#{att}=", to: class_to.underscore.to_sym)
76
+ end
75
77
 
76
- # Create required methods to use `fields_for`
77
- def set_fields_for_methods assoc_set, singular = false
78
- assoc_set.each do |c|
79
- #ap "criando método #{c[:name]}"
80
- self.class.send(:define_method, c[:name]) do
81
- assocs = instance_variable_get("@#{get_main_model.to_s.underscore}")
82
- .send(c[:name])
83
-
84
- neww = singular ? c[:klass].constantize.new : [c[:klass].constantize.new] * 2
85
-
86
- if singular
87
- (assocs.present? && assocs) || neww
88
- else
89
- (assocs.map{|c| c}.present? && assocs.map{|c| c}) || neww
90
- end
91
- end
78
+ # Create required methods to use `fields_for`
79
+ def set_fields_for_methods(assoc_set, singular = false)
80
+ assoc_set.each do |c|
81
+ # ap "creating method #{c[:name]}"
82
+ self.class.send(:define_method, c[:name]) do
83
+ assocs = instance_variable_get("@#{get_main_model.to_s.underscore}")
84
+ .send(c[:name])
92
85
 
93
- #ap "criando método #{c[:name]}_attributes="
94
- self.class.send(:define_method, "#{c[:name]}_attributes=") do |attributes|
95
- end
96
- end
97
- end
86
+ neww = singular ? c[:klass].constantize.new : [c[:klass].constantize.new] * 2
98
87
 
99
- def set_non_fields_for_methods assoc_set
100
- assoc_set.each do |c|
101
- #ap "criando método #{c[:name]}_list"
102
- self.class.send(:define_method, "#{c[:name]}_list") do
103
- assoc = instance_variable_get("@#{get_main_model.to_s.underscore}")
104
- .send(c[:name])
105
- assoc.present? && assoc.id || nil
88
+ if singular
89
+ (assocs.present? && assocs) || neww
90
+ else
91
+ (assocs.map { |c| c }.present? && assocs.map { |c| c }) || neww
106
92
  end
107
93
  end
108
- end
109
94
 
110
- def set_remove_accessor assoc_set
111
- assoc_set.each do |c|
112
- #ap "Criando attr_accessor :_remove para #{c[:klass]}"
113
- c[:klass].constantize.class_eval{ attr_accessor :_remove }
95
+ # ap "creating method #{c[:name]}_attributes="
96
+ self.class.send(:define_method, "#{c[:name]}_attributes=") do |attributes|
114
97
  end
115
98
  end
99
+ end
116
100
 
117
- def map_associations assoc
118
- assoc.inject([]) do |t, c|
119
- t << {
120
- name: c.name.to_s,
121
- klass: c.klass.name,
122
- # delete_if remove useless attrs
123
- columns: filter_columns(c.klass)
124
- }
101
+ def set_non_fields_for_methods(assoc_set)
102
+ assoc_set.each do |c|
103
+ # ap "creating method #{c[:name]}_list"
104
+ self.class.send(:define_method, "#{c[:name]}_list") do
105
+ assoc = instance_variable_get("@#{get_main_model.to_s.underscore}")
106
+ .send(c[:name])
107
+ assoc.present? && assoc.id || nil
125
108
  end
126
109
  end
110
+ end
127
111
 
128
- def map_has_many_associations
129
- # Create an array with associated classes names and attrs
130
- @mapped_hm_assoc ||= map_associations(get_has_many_associations)
131
- @mapped_hm_assoc
112
+ def set_remove_accessor(assoc_set)
113
+ assoc_set.each do |c|
114
+ # ap "creating attr_accessor :_remove para #{c[:klass]}"
115
+ c[:klass].constantize.class_eval { attr_accessor :_remove }
132
116
  end
117
+ end
133
118
 
134
- def map_belongs_to_associations
135
- # Create an array with associated classes names and attrs
136
- @mapped_bt_assoc ||= map_associations(get_belongs_to_associations)
137
- @mapped_bt_assoc
119
+ def map_associations(assoc)
120
+ assoc.inject([]) do |t, c|
121
+ t << {
122
+ name: c.name.to_s,
123
+ klass: c.klass.name,
124
+ # delete_if remove useless attrs
125
+ columns: filter_columns(c.klass)
126
+ }
138
127
  end
128
+ end
139
129
 
140
- def map_has_one_associations
141
- # Create an array with associated classes names and attrs
142
- @mapped_ho_assoc ||= map_associations(get_has_one_associations)
143
- @mapped_ho_assoc
144
- end
130
+ def map_has_many_associations
131
+ # Create an array with associated classes names and attrs
132
+ @mapped_hm_assoc ||= map_associations(get_has_many_associations)
133
+ @mapped_hm_assoc
134
+ end
135
+
136
+ def map_belongs_to_associations
137
+ # Create an array with associated classes names and attrs
138
+ @mapped_bt_assoc ||= map_associations(get_belongs_to_associations)
139
+ @mapped_bt_assoc
140
+ end
141
+
142
+ def map_has_one_associations
143
+ # Create an array with associated classes names and attrs
144
+ @mapped_ho_assoc ||= map_associations(get_has_one_associations)
145
+ @mapped_ho_assoc
146
+ end
145
147
  end
146
- end
148
+ end
@@ -1,9 +1,9 @@
1
1
  module Linker
2
2
  module ConfigurationMethods
3
3
  extend ActiveSupport::Concern
4
-
4
+
5
5
  module ClassMethods
6
- def main_model main_model
6
+ def main_model(main_model)
7
7
  @main_model = main_model.to_s.camelize
8
8
  end
9
9
 
@@ -19,6 +19,5 @@ module Linker
19
19
  include ActiveModel::Validations
20
20
  end
21
21
  end
22
-
23
22
  end
24
- end
23
+ end
@@ -5,13 +5,13 @@ module Linker
5
5
  extend ActiveSupport::Concern
6
6
 
7
7
  included do
8
- def params= params
8
+ def params=(params)
9
9
  before_set_params(params)
10
10
  params.each do |param, value|
11
11
  if value.is_a?(Hash)
12
12
  table = param.gsub(%r{_attributes$}, '')
13
13
  # belongs_to attrs
14
- if map_belongs_to_associations.select{|c| c[:name] == table}.present?
14
+ if map_belongs_to_associations.select { |c| c[:name] == table }.present?
15
15
  if value['id'].present?
16
16
  _get_main_model.send(table).update_attributes(value)
17
17
  else
@@ -28,12 +28,12 @@ module Linker
28
28
 
29
29
  # has_many attrs
30
30
  else
31
- ids_to_remove = value.map{|c| c.last['id'] if c.last['id'].present? && c.last.key?('_remove') && c.last['_remove'] == '1' }.compact
31
+ ids_to_remove = value.map { |c| c.last['id'] if c.last['id'].present? && c.last.key?('_remove') && c.last['_remove'] == '1' }.compact
32
32
 
33
33
  if ids_to_remove.present?
34
34
  r = search_has_many(table)
35
35
  r[:klass].constantize.send(:where, ["#{r[:klass].constantize.table_name}.id IN (?)", ids_to_remove]).destroy_all
36
- value.delete_if{|i, c| ids_to_remove.include?(c['id']) }
36
+ value.delete_if { |i, c| ids_to_remove.include?(c['id']) }
37
37
  end
38
38
 
39
39
  value.each do |c|
@@ -63,26 +63,28 @@ module Linker
63
63
  # @param validate [boolean] a boolean declaring if the form class must be validated.
64
64
  # @return [boolean] a boolean representing if the form class was validated (or no, if `validate` is `false`) and
65
65
  # saved successfully
66
- def save validate: true
67
- main_model = _get_main_model
68
-
69
- valid = true
70
- if validate
71
- valid = self.valid?
72
- if valid
66
+ def save(validate: true)
67
+ ActiveRecord::Base.transaction do
68
+ main_model = _get_main_model
69
+
70
+ valid = true
71
+ if validate
72
+ valid = self.valid?
73
+ if valid
74
+ before_save
75
+ save = main_model.save
76
+ after_save
77
+ end
78
+ else
73
79
  before_save
74
80
  save = main_model.save
75
81
  after_save
76
82
  end
77
- else
78
- before_save
79
- save = main_model.save
80
- after_save
83
+ valid && save
81
84
  end
82
- valid && save
83
85
  end
84
86
 
85
- def before_set_params params
87
+ def before_set_params(params)
86
88
  end
87
89
 
88
90
  def before_save
@@ -90,22 +92,22 @@ module Linker
90
92
 
91
93
  def after_save
92
94
  end
93
-
94
95
  end
95
96
 
96
97
  private
97
- def _get_main_model
98
- main_model ||= self.send(self.class._main_model.underscore)
99
- end
100
98
 
101
- def search_has_one name
102
- s = @mapped_ho_assoc.detect{|c| c[:name] == name}
103
- s.present? && s
104
- end
99
+ def _get_main_model
100
+ main_model ||= self.send(self.class._main_model.underscore)
101
+ end
105
102
 
106
- def search_has_many name
107
- s = @mapped_hm_assoc.detect{|c| c[:name] == name}
108
- s.present? && s
109
- end
103
+ def search_has_one(name)
104
+ s = @mapped_ho_assoc.detect { |c| c[:name] == name }
105
+ s.present? && s
106
+ end
107
+
108
+ def search_has_many(name)
109
+ s = @mapped_hm_assoc.detect { |c| c[:name] == name }
110
+ s.present? && s
111
+ end
110
112
  end
111
- end
113
+ end
@@ -1,3 +1,3 @@
1
1
  module Linker
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
data/lib/linker.rb CHANGED
@@ -3,12 +3,12 @@ require 'active_support/all'
3
3
  require 'linker/forms/attributes'
4
4
  require 'linker/forms/params'
5
5
 
6
- module Linker
6
+ module Linker
7
7
  extend ActiveSupport::Concern
8
8
  include Linker::Attributes
9
9
  include Linker::Params
10
10
 
11
- def initialize main_model_instance = self.class._main_model.constantize.send(:new)
11
+ def initialize(main_model_instance = self.class._main_model.constantize.send(:new))
12
12
  # Creating instance variable for main model
13
13
  instance_variable_set("@#{main_model_instance.class.name.underscore}", main_model_instance)
14
14
 
@@ -25,4 +25,4 @@ module Linker
25
25
  instance_variable_get("@#{self.class._main_model.underscore}")
26
26
  end
27
27
  end
28
- end
28
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: linker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glauco Custódio
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-09 00:00:00.000000000 Z
11
+ date: 2016-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -178,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  version: '0'
179
179
  requirements: []
180
180
  rubyforge_project:
181
- rubygems_version: 2.4.6
181
+ rubygems_version: 2.5.1
182
182
  signing_key:
183
183
  specification_version: 4
184
184
  summary: A wrapper to form objects in ActiveRecord. Forget accepts_nested_attributes_for.
@@ -193,4 +193,3 @@ test_files:
193
193
  - spec/fake_app/rails_app.rb
194
194
  - spec/spec_helper.rb
195
195
  - spec/users_form_spec.rb
196
- has_rdoc: