forest_liana 1.6.5 → 1.6.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 65bc0a94633ab9554e51ace04fb84e302b2afb0b
4
- data.tar.gz: f026a0b61db387f8045b4f50a48ba54288e75b20
3
+ metadata.gz: ce8dc1b8cf30b7f5734362bcd69b549d990a2b71
4
+ data.tar.gz: 5aca5a5e19a92349d6503a0f8b3b72b3cbc40ab6
5
5
  SHA512:
6
- metadata.gz: 80fd90903e69868ed3132c6b5a49b9526922ca38a7961b415fcf977db027c03589f3142ed77b39511921aef38061b6928cd774322d7981ac10eb260197ebd739
7
- data.tar.gz: c818a6da4a756d1deafbb07caef9299866fea4d72fb85aa20ae49d68017abc5f25d626a6c39970e48e355bb9d150fe628f69caf21a8ce791aa2d6e9bec48149d
6
+ metadata.gz: 3e7c2b6a3afbe0c786ac13efa22874e400cedbe85f8e9fb7ff076d83335f49db441239effcf598b705b37320a72057d7b0f6ac697f51b3040f335b209774239c
7
+ data.tar.gz: 1567b0fe318660845cf31b843eaafe32ab09d79ab061cd526cfeb3e863900a35f273e23c9bd4d3addf8eca1c83ca8537bbc1b99a9ad5c6134f855daedc8f577e
@@ -42,7 +42,7 @@ module ForestLiana
42
42
  private
43
43
 
44
44
  def find_resource
45
- @resource = SchemaUtils.find_model_from_table_name(params[:collection])
45
+ @resource = SchemaUtils.find_model_from_collection_name(params[:collection])
46
46
 
47
47
  if @resource.nil? || !@resource.ancestors.include?(ActiveRecord::Base)
48
48
  render serializer: nil, json: {status: 404}, status: :not_found
@@ -1,7 +1,7 @@
1
1
  module ForestLiana
2
2
  class DeviseController < ForestLiana::ApplicationController
3
3
  def change_password
4
- resource = SchemaUtils.find_model_from_table_name(
4
+ resource = SchemaUtils.find_model_from_collection_name(
5
5
  params['data']['attributes']['collection_name'])
6
6
 
7
7
  user = resource.find(params['data']['attributes']['ids'].first)
@@ -72,7 +72,7 @@ module ForestLiana
72
72
  private
73
73
 
74
74
  def find_resource
75
- @resource = SchemaUtils.find_model_from_table_name(params[:collection])
75
+ @resource = SchemaUtils.find_model_from_collection_name(params[:collection])
76
76
 
77
77
  if @resource.nil? || !SchemaUtils.model_included?(@resource) ||
78
78
  !@resource.ancestors.include?(ActiveRecord::Base)
@@ -1,9 +1,10 @@
1
1
  class ForestLiana::Router
2
2
  def call(env)
3
3
  params = env['action_dispatch.request.path_parameters']
4
- resource = ForestLiana::SchemaUtils.find_model_from_table_name(params[:collection])
4
+ resource = ForestLiana::SchemaUtils.find_model_from_collection_name(
5
+ params[:collection])
5
6
 
6
- class_name = resource.table_name.classify
7
+ class_name = ForestLiana.name_for(resource).classify
7
8
  module_name = class_name.deconstantize
8
9
 
9
10
  name = module_name if module_name
@@ -27,7 +27,8 @@ module ForestLiana
27
27
  private
28
28
 
29
29
  def find_resource
30
- @resource = SchemaUtils.find_model_from_table_name(params[:collection])
30
+ @resource = SchemaUtils.find_model_from_collection_name(
31
+ params[:collection])
31
32
 
32
33
  if @resource.nil? || !@resource.ancestors.include?(ActiveRecord::Base)
33
34
  render json: {status: 404}, status: :not_found, serializer: nil
@@ -4,7 +4,7 @@ module ForestLiana
4
4
  class SerializerFactory
5
5
 
6
6
  def self.define_serializer(active_record_class, serializer)
7
- class_name = active_record_class.table_name.classify
7
+ class_name = ForestLiana.name_for(active_record_class).classify
8
8
  module_name = class_name.deconstantize
9
9
 
10
10
  name = module_name if module_name
@@ -46,7 +46,7 @@ module ForestLiana
46
46
  elsif active_record_class == ForestLiana::Model::Segment
47
47
  "ForestLiana::SegmentSerializer"
48
48
  else
49
- class_name = active_record_class.table_name.classify
49
+ class_name = ForestLiana.name_for(active_record_class).classify
50
50
  module_name = class_name.deconstantize
51
51
 
52
52
  name = module_name if module_name
@@ -65,7 +65,7 @@ module ForestLiana
65
65
  end
66
66
 
67
67
  def type
68
- object.class.table_name.demodulize
68
+ ForestLiana.name_for(object.class).demodulize
69
69
  end
70
70
 
71
71
  def format_name(attribute_name)
@@ -86,27 +86,27 @@ module ForestLiana
86
86
  # Has many smart field
87
87
  current = self.has_many_relationships[attribute_name]
88
88
  if current.try(:[], :options).try(:[], :name) == attribute_name
89
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/#{attribute_name}"
89
+ ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/#{attribute_name}"
90
90
  return ret
91
91
  end
92
92
 
93
93
  if intercom_integration?
94
94
  case attribute_name
95
95
  when :intercom_conversations
96
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/intercom_conversations"
96
+ ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/intercom_conversations"
97
97
  when :intercom_attributes
98
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/intercom_attributes"
98
+ ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/intercom_attributes"
99
99
  end
100
100
  end
101
101
 
102
102
  if stripe_integration?
103
103
  case attribute_name
104
104
  when :stripe_payments
105
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/stripe_payments"
105
+ ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_payments"
106
106
  when :stripe_invoices
107
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/stripe_invoices"
107
+ ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_invoices"
108
108
  when :stripe_cards
109
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/stripe_cards"
109
+ ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/stripe_cards"
110
110
  end
111
111
  end
112
112
 
@@ -118,7 +118,7 @@ module ForestLiana
118
118
 
119
119
  SchemaUtils.many_associations(object.class).each do |a|
120
120
  if a.name == attribute_name
121
- ret[:href] = "/forest/#{object.class.table_name}/#{object.id}/relationships/#{attribute_name}"
121
+ ret[:href] = "/forest/#{ForestLiana.name_for(object.class)}/#{object.id}/relationships/#{attribute_name}"
122
122
  end
123
123
  end
124
124
  rescue TypeError, ActiveRecord::StatementInvalid, NoMethodError
@@ -2,7 +2,7 @@ module ForestLiana
2
2
  class ControllerFactory
3
3
 
4
4
  def self.define_controller(active_record_class, service)
5
- class_name = active_record_class.table_name.classify
5
+ class_name = ForestLiana.name_for(active_record_class).classify
6
6
  module_name = class_name.deconstantize
7
7
 
8
8
  name = module_name if module_name
@@ -12,7 +12,7 @@ module ForestLiana
12
12
  end
13
13
 
14
14
  def self.get_controller_name(active_record_class)
15
- class_name = active_record_class.table_name.classify
15
+ class_name = ForestLiana.name_for(active_record_class).classify
16
16
  module_name = class_name.deconstantize
17
17
 
18
18
  name = module_name if module_name
@@ -50,12 +50,12 @@ module ForestLiana
50
50
  def collection
51
51
  @collection ||= begin
52
52
  collection = ForestLiana.apimap.find do |x|
53
- x.name.to_s == @model.table_name
53
+ x.name.to_s == ForestLiana.name_for(@model)
54
54
  end
55
55
 
56
56
  if collection.blank?
57
57
  collection = ForestLiana::Model::Collection.new({
58
- name: @model.table_name,
58
+ name: ForestLiana.name_for(@model),
59
59
  fields: []
60
60
  })
61
61
 
@@ -80,7 +80,7 @@ module ForestLiana
80
80
  if ForestLiana.integrations.try(:[], :intercom)
81
81
  .try(:[], :mapping).try(:include?, @model.name)
82
82
 
83
- model_name = @model.table_name
83
+ model_name = ForestLiana.name_for(@model)
84
84
 
85
85
  collection.fields << {
86
86
  field: :intercom_conversations,
@@ -110,7 +110,7 @@ module ForestLiana
110
110
  .select { |mapping| mapping.split('.')[0] == @model.name }
111
111
  .size > 0
112
112
 
113
- model_name = @model.table_name
113
+ model_name = ForestLiana.name_for(@model)
114
114
 
115
115
  collection.fields << {
116
116
  field: :stripe_payments,
@@ -235,7 +235,7 @@ module ForestLiana
235
235
  {
236
236
  field: association.name.to_s,
237
237
  type: get_type_for_association(association),
238
- reference: "#{association.klass.table_name}.id",
238
+ reference: "#{ForestLiana.name_for(association.klass)}.id",
239
239
  inverseOf: inverse_of(association)
240
240
  }
241
241
  end
@@ -298,7 +298,7 @@ module ForestLiana
298
298
  if association.options[:polymorphic] == true
299
299
  '*.id'
300
300
  else
301
- "#{association.klass.table_name.underscore}.id"
301
+ "#{ForestLiana.name_for(association.klass).underscore}.id"
302
302
  end
303
303
  end
304
304
 
@@ -19,13 +19,13 @@ module ForestLiana
19
19
  end
20
20
  end
21
21
 
22
- def self.find_model_from_table_name(table_name)
22
+ def self.find_model_from_collection_name(collection_name)
23
23
  model = nil
24
24
 
25
25
  ForestLiana.models.each do |subclass|
26
26
  if subclass.abstract_class?
27
- model = self.find_model_from_abstract_class(subclass, table_name)
28
- elsif subclass.table_name == table_name
27
+ model = self.find_model_from_abstract_class(subclass, collection_name)
28
+ elsif ForestLiana.name_for(subclass) == collection_name
29
29
  model = subclass.base_class
30
30
  end
31
31
 
@@ -45,12 +45,12 @@ module ForestLiana
45
45
  association.options[:polymorphic]
46
46
  end
47
47
 
48
- def self.find_model_from_abstract_class(abstract_class, table_name)
48
+ def self.find_model_from_abstract_class(abstract_class, collection_name)
49
49
  abstract_class.subclasses.find do |subclass|
50
50
  if subclass.abstract_class?
51
- return self.find_model_from_table_name(subclass, table_name)
51
+ return self.find_model_from_collection_name(subclass, collection_name)
52
52
  else
53
- subclass.table_name == table_name
53
+ ForestLiana.name_for(subclass) == collection_name
54
54
  end
55
55
  end
56
56
  end
@@ -75,7 +75,7 @@ module ForestLiana
75
75
  begin
76
76
  parent = model.try(:superclass)
77
77
 
78
- if parent.try(:table_name)
78
+ if ForestLiana.name_for(parent)
79
79
  inheritance_column = parent.columns.find do |c|
80
80
  (parent.inheritance_column && c.name == parent.inheritance_column)\
81
81
  || c.name == 'type'
@@ -79,7 +79,7 @@ module ForestLiana
79
79
  def field
80
80
  ForestLiana.integrations[:stripe][:mapping].select { |value|
81
81
  value.split('.')[0] == ForestLiana::SchemaUtils
82
- .find_model_from_table_name(@params[:collection]).try(:name)
82
+ .find_model_from_collection_name(@params[:collection]).try(:name)
83
83
  }.first.split('.')[1]
84
84
  end
85
85
  end
@@ -79,7 +79,7 @@ module ForestLiana
79
79
  def field
80
80
  ForestLiana.integrations[:stripe][:mapping].select { |value|
81
81
  value.split('.')[0] == ForestLiana::SchemaUtils
82
- .find_model_from_table_name(@params[:collection]).try(:name)
82
+ .find_model_from_collection_name(@params[:collection]).try(:name)
83
83
  }.first.split('.')[1]
84
84
  end
85
85
  end
@@ -88,7 +88,7 @@ module ForestLiana
88
88
  def field
89
89
  ForestLiana.integrations[:stripe][:mapping].select { |value|
90
90
  value.split('.')[0] == ForestLiana::SchemaUtils
91
- .find_model_from_table_name(@params[:collection]).try(:name)
91
+ .find_model_from_collection_name(@params[:collection]).try(:name)
92
92
  }.first.split('.')[1]
93
93
  end
94
94
  end
@@ -86,7 +86,7 @@ module ForestLiana
86
86
  def field
87
87
  ForestLiana.integrations[:stripe][:mapping].select { |value|
88
88
  value.split('.')[0] == ForestLiana::SchemaUtils
89
- .find_model_from_table_name(@params[:collection]).try(:name)
89
+ .find_model_from_collection_name(@params[:collection]).try(:name)
90
90
  }.first.split('.')[1]
91
91
  end
92
92
  end
@@ -90,7 +90,7 @@ module ForestLiana
90
90
  def field
91
91
  ForestLiana.integrations[:stripe][:mapping].select { |value|
92
92
  value.split('.')[0] == ForestLiana::SchemaUtils
93
- .find_model_from_table_name(@params[:collection]).try(:name)
93
+ .find_model_from_collection_name(@params[:collection]).try(:name)
94
94
  }.first.split('.')[1]
95
95
  end
96
96
  end
data/lib/forest_liana.rb CHANGED
@@ -20,6 +20,7 @@ module ForestLiana
20
20
  mattr_accessor :excluded_models
21
21
  mattr_accessor :included_models
22
22
  mattr_accessor :user_class_name
23
+ mattr_accessor :names_overriden
23
24
 
24
25
  self.apimap = []
25
26
  self.allowed_users = []
@@ -27,8 +28,16 @@ module ForestLiana
27
28
  self.excluded_models = []
28
29
  self.included_models = []
29
30
  self.user_class_name = nil
31
+ self.names_overriden = {}
30
32
 
31
33
  def self.schema_for_resource resource
32
- self.apimap.find {|collection| collection.name == resource.table_name}
34
+ self.apimap.find do |collection|
35
+ SchemaUtils.find_model_from_collection_name(collection.name)
36
+ .try(:table_name) == resource.table_name
37
+ end
38
+ end
39
+
40
+ def self.name_for(model)
41
+ self.names_overriden[model] || model.try(:table_name)
33
42
  end
34
43
  end
@@ -20,6 +20,7 @@ module ForestLiana
20
20
  def perform
21
21
  fetch_models
22
22
  check_integrations_setup
23
+ namespace_duplicated_models
23
24
  create_factories
24
25
 
25
26
  if ForestLiana.env_secret
@@ -100,6 +101,19 @@ module ForestLiana
100
101
  end
101
102
  end
102
103
 
104
+ def namespace_duplicated_models
105
+ ForestLiana.models
106
+ .group_by {|model| model.table_name}
107
+ .select {|table_name, models| models.length > 1}
108
+ .try(:each) do |table_name, models|
109
+ models.each do |model|
110
+ ForestLiana.names_overriden[model] =
111
+ "#{model.name.deconstantize.downcase}__#{model.table_name}"
112
+ end
113
+ end
114
+
115
+ end
116
+
103
117
  def create_apimap
104
118
  ForestLiana.models.map do |model|
105
119
  if analyze_model?(model)
@@ -100,7 +100,8 @@ module ForestLiana::Collection
100
100
  end
101
101
 
102
102
  def active_record_class
103
- ForestLiana::SchemaUtils.find_model_from_table_name(self.collection_name)
103
+ ForestLiana::SchemaUtils.find_model_from_collection_name(
104
+ self.collection_name)
104
105
  end
105
106
 
106
107
  def serializer_name
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "1.6.5"
2
+ VERSION = "1.6.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.5
4
+ version: 1.6.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-12 00:00:00.000000000 Z
11
+ date: 2017-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails