dynamic-active-model 0.4.6 → 0.5.1

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
  SHA256:
3
- metadata.gz: 6ef466cf4634fec36648e25312d9a4729988584a86cb5dfc071c49b5515a5845
4
- data.tar.gz: 895ea1c5eb60706bbc795c75b4f15aca8e833d3cb56d390985973d4d32053e0f
3
+ metadata.gz: 4f189718e66fe6718863a858f26246214c0faf3797c629798cd1fe1ad0618e72
4
+ data.tar.gz: 466b853aa02d38e9fd03a46feaca892779ba05142330a4942b704e007b3faffe
5
5
  SHA512:
6
- metadata.gz: 0c2055798c8e10c2f3d1172f9b9c7d99983cd61da0a88b512528e8c43b2d309179d5cb6c15f00890c66c9fd1c29e8b1e34ed5ba3c0e0d103418f9d11b22e167a
7
- data.tar.gz: 2d5e76d0d77276fd867b160d33ebfa59d2cb790a5968a30b8a94942fecda9aadcd6703c5b58b2d12bb5d8cd673b264ff472a00398a391f959b7670a060570f18
6
+ metadata.gz: 9fbfaa0f9abde1b6a49101e48ad2f3c7f671afad6594e25b1af74dabf7bfcd9f284822b0a6dab5cd2f7ddba6d0430e007b4323284707c44d58719414d6bcd2e8
7
+ data.tar.gz: e37667538308d62312f5aade7c712b872f2c6aa86a9fd0bdfa53dae535cf638b582deb19b604b58ad62055ecfb45a0d6d4d80952dabe69ec094a3d92cb4f4a3c
@@ -8,8 +8,8 @@ module DynamicActiveModel
8
8
  if base.attribute_names
9
9
  columns_to_ignore = base.columns.select do |column|
10
10
  if column.type == :boolean
11
- base.dangerous_attribute_method?(column.name)
12
- base.dangerous_attribute_method?(column.name + '?')
11
+ base.dangerous_attribute_method?(column.name) ||
12
+ base.dangerous_attribute_method?(column.name + '?')
13
13
  else
14
14
  base.dangerous_attribute_method?(column.name)
15
15
  end
@@ -8,6 +8,12 @@ module DynamicActiveModel
8
8
  :factory,
9
9
  :models
10
10
 
11
+ class ModelUpdater < Struct.new(:model)
12
+ def update_model(&block)
13
+ model.class_eval(&block)
14
+ end
15
+ end
16
+
11
17
  def initialize(base_module, connection_options, base_class_name = nil)
12
18
  @factory = Factory.new(base_module, connection_options, base_class_name)
13
19
  @table_class_names = {}
@@ -70,6 +76,34 @@ module DynamicActiveModel
70
76
  end
71
77
  alias disable_sti! disable_standard_table_inheritance!
72
78
 
79
+ def get_model(table_name)
80
+ table_name = table_name.to_s
81
+ models.detect { |model| model.table_name == table_name }
82
+ end
83
+
84
+ def get_model!(table_name)
85
+ model = get_model(table_name)
86
+ return model if model
87
+
88
+ raise ::DynamicActiveModel::ModelNotFound.new("no model found for table #{table_name}")
89
+ end
90
+
91
+ def update_model(table_name, file = nil, &block)
92
+ model = get_model!(table_name)
93
+ ModelUpdater.new(model).instance_eval(File.read(file)) if file
94
+ model.class_eval(&block) if block
95
+ model
96
+ end
97
+
98
+ def update_all_models(base_dir, ext='.ext.rb')
99
+ Dir.glob("#{base_dir}/*#{ext}") do |file|
100
+ next unless File.file?(file)
101
+
102
+ table_name = File.basename(file).split('.', 2).first
103
+ update_model(table_name, file)
104
+ end
105
+ end
106
+
73
107
  private
74
108
 
75
109
  def skip_table?(table_name)
@@ -23,7 +23,6 @@ module DynamicActiveModel
23
23
  include DynamicActiveModel::DangerousAttributesPatch
24
24
  end
25
25
  @base_module.const_set(class_name, kls)
26
- require_extension(class_name)
27
26
  @base_module.const_get(class_name)
28
27
  end
29
28
 
@@ -53,12 +52,5 @@ module DynamicActiveModel
53
52
 
54
53
  class_name
55
54
  end
56
-
57
- def require_extension(class_name)
58
- file = File.expand_path(DynamicActiveModel.base_models_path + '/' + class_name.underscore + '.rb')
59
- return unless File.exist?(file)
60
-
61
- require file
62
- end
63
55
  end
64
56
  end
@@ -10,11 +10,5 @@ module DynamicActiveModel
10
10
  autoload :Associations, 'dynamic-active-model/associations'
11
11
  autoload :TemplateClassFile, 'dynamic-active-model/template_class_file'
12
12
 
13
- def self.base_models_path
14
- @base_models_path || 'app/models'
15
- end
16
-
17
- def self.base_models_path=(path)
18
- @base_models_path = path
19
- end
13
+ class ModelNotFound < Exception; end
20
14
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dynamic-active-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doug Youch
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-04 00:00:00.000000000 Z
11
+ date: 2024-10-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
59
59
  - !ruby/object:Gem::Version
60
60
  version: '0'
61
61
  requirements: []
62
- rubygems_version: 3.3.3
62
+ rubygems_version: 3.5.3
63
63
  signing_key:
64
64
  specification_version: 4
65
65
  summary: Dynamic ActiveRecord Models