modalfields 1.2.2 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,8 +1,11 @@
1
1
  source "http://rubygems.org"
2
+
3
+ gem 'modalsettings'
2
4
  gem "activesupport", ">= 2.3.5"
3
5
  gem "activerecord", ">= 2.3.5"
4
6
  # gem "rails", ">= 2.3.5"
5
7
 
8
+
6
9
  # Add dependencies to develop your gem here.
7
10
  # Include everything needed to run rake, tests, features, etc.
8
11
  group :development do
data/Gemfile.lock CHANGED
@@ -21,6 +21,9 @@ GEM
21
21
  rake
22
22
  rdoc
23
23
  json (1.6.6)
24
+ modalsettings (1.0.0)
25
+ modalsupport (>= 0.8.1)
26
+ modalsupport (0.9.2)
24
27
  pg (0.10.1)
25
28
  rake (0.9.2.2)
26
29
  rdoc (3.12)
@@ -37,6 +40,7 @@ DEPENDENCIES
37
40
  activesupport (>= 2.3.5)
38
41
  bundler (~> 1)
39
42
  jeweler (~> 1.8.3)
43
+ modalsettings
40
44
  pg
41
45
  rdoc (~> 3.12)
42
46
  shoulda
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.2.2
1
+ 1.3.0
@@ -1,3 +1,5 @@
1
+ require 'modalsettings'
2
+
1
3
  # This is a hybrid between HoboFields and model annotators.
2
4
  #
3
5
  # It works like other annotators, by updating the model annotations from the DB schema.
@@ -63,7 +65,6 @@ module ModalFields
63
65
 
64
66
  end
65
67
 
66
-
67
68
  class DefinitionsDsl < DslBase
68
69
  def field(name, attributes={})
69
70
  ModalFields.definitions[name.to_sym] = COMMON_ATTRIBUTES.merge(attributes)
@@ -141,8 +142,14 @@ module ModalFields
141
142
  @definitions = {}
142
143
  @column_to_field_declaration_hook = nil
143
144
  @type_aliases = {}
145
+ @parameters = Settings[
146
+ :check_all_models => false, # consider all models (including those defined in plugins) for migration & check
147
+ :check_dynamic_models => false, # consider models that may have been defined dynamically not in model files
148
+ :update_vendor_models => false # update also model files in vendor
149
+ ]
144
150
 
145
151
  class <<self
152
+
146
153
  attr_reader :hooks, :definitions
147
154
  # Define declaration of primary keys
148
155
  # ModalFields.show_primary_keys = false # the default: do not show primary keys
@@ -151,6 +158,11 @@ module ModalFields
151
158
  # ModalFields.show_primary_keys = :except_id # only declare if named differently from 'id'
152
159
  attr_accessor :show_primary_keys
153
160
 
161
+ def parameters(params=nil)
162
+ @parameters.merge! params if params
163
+ @parameters
164
+ end
165
+
154
166
  # Run a definition block that executes field type definitions
155
167
  def define(&blk)
156
168
  DefinitionsDsl.new.instance_eval(&blk)
@@ -186,7 +198,7 @@ module ModalFields
186
198
  # It is recommended to run this on a clearn working directory (no uncommitted changes), so that the
187
199
  # changes can be easily reviewed.
188
200
  def update(modify=true)
189
- dbmodels.each do |model, file|
201
+ dbmodels(dbmodel_options).each do |model, file|
190
202
  next if file.nil?
191
203
  new_fields, modified_fields, deleted_fields, deleted_model = diff(model)
192
204
  unless new_fields.empty? && modified_fields.empty? && deleted_fields.empty?
@@ -221,7 +233,7 @@ module ModalFields
221
233
  end
222
234
 
223
235
  def check
224
- dbmodels.each do |model, file|
236
+ dbmodels(dbmodel_options).each do |model, file|
225
237
  new_fields, modified_fields, deleted_fields, deleted_model = diff(model)
226
238
  unless new_fields.empty? && modified_fields.empty? && deleted_fields.empty?
227
239
  rel_file = file && file.sub(/\A#{Rails.root}/,'')
@@ -239,7 +251,7 @@ module ModalFields
239
251
  def migration
240
252
  up = ""
241
253
  down = ""
242
- dbmodels.each do |model, file|
254
+ dbmodels(dbmodel_options).each do |model, file|
243
255
  new_fields, modified_fields, deleted_fields, deleted_model = diff(model)
244
256
  unless new_fields.empty? && modified_fields.empty? && deleted_fields.empty?
245
257
  up << "\n"
@@ -311,20 +323,84 @@ module ModalFields
311
323
 
312
324
  private
313
325
 
314
- # return ActiveRecord classes corresponding to tables, without STI derived classes, but including indirectly
315
- # derived classes that do have their own tables (to achieve this we use the convention that in such cases
316
- # the base class, directly derived from ActiveRecord::Base has a nil table_name)
317
- def dbmodels
318
- models = Dir.glob(File.join(Rails.root,"app/models/**/*.rb"))\
319
- .map{|f| [File.basename(f).chomp(".rb").camelize.constantize, f]}\
320
- .select{|c,f| has_table(c)}\
321
- .reject{|c,f| has_table(c.superclass)}
322
- models += ActiveRecord::Base.send(:subclasses).reject{|c| c.name.starts_with?('CGI::') || !has_table(c) || has_table(c.superclass)}
323
- models.uniq
326
+ # Return the database models
327
+ # Options:
328
+ # :all_models # Return also models in plugins, not only in the app (app/models)
329
+ # :dynamic_models # Return dynamically defined models too (not defined in a model file)
330
+ # :exclude_sti_models # Exclude derived (STI) models
331
+ # :exclude_non_sti_models # Exclude top level models
332
+ # :include_files # Return also the model definition file pathnames (return pairs of [model, file])
333
+ # :only_app_files # But return nil for files not in the app proper
334
+ # :only_app_tree_files # But return nil for files not in the app directory tree (app, vendor...)
335
+ def dbmodels(options={})
336
+
337
+ models_dir = 'app/models'
338
+ if Rails.respond_to?(:application)
339
+ models_dir = Rails.application.paths[models_dir]
340
+ end
341
+ models_dir = Rails.root.join(models_dir)
342
+
343
+ if options[:all_models]
344
+ # Include also models from plugins
345
+ model_dirs = $:.grep(/\/models\/?\Z/)
346
+ else
347
+ # Only main application models
348
+ model_dirs = [models_dir]
349
+ end
350
+
351
+ models = []
352
+ files = {}
353
+ model_dirs.each do |base|
354
+ Dir.glob(File.join(base,"**/*.rb")).each do |fn|
355
+ model = File.basename(fn).chomp(".rb").camelize.constantize
356
+ models << model
357
+ files[model.to_s] = fn
358
+ end
359
+ end
360
+ models = models.sort_by{|m| m.to_s}
361
+
362
+ if options[:dynamic_models]
363
+ # Now add dynamically generated models (not having dedicated files)
364
+ # note that subclasses of these models are not added here
365
+ models += ActiveRecord::Base.send(:subclasses)
366
+ models = models.uniq
367
+ end
368
+
369
+ models = models.uniq.reject{|model| !has_table?(model)}
370
+
371
+ non_sti_models, sti_models = models.partition{|model| model.base_class==model}
372
+
373
+ models = []
374
+ models += non_sti_models unless options[:exclude_non_sti_models]
375
+ models += sti_models unless options[:exclude_sti_models]
376
+ if options[:include_files]
377
+ models = models.map{|model| [model, files[model.to_s]]}
378
+ if options[:only_app_files] || options[:only_app_tree_files]
379
+ if options[:only_app_files]
380
+ suffix = models_dir.to_s
381
+ else
382
+ suffix = Rails.root.to_s
383
+ end
384
+ suffix += '/' unless suffix.ends_with?('/')
385
+ models = models.map{|model, file| [model, file && (file.starts_with?(suffix) ? file : nil)]}
386
+ end
387
+ end
388
+ models
389
+ end
390
+
391
+ def has_table?(cls)
392
+ (cls != ActiveRecord::Base) && cls.respond_to?(:table_name) && cls.table_name.present?
324
393
  end
325
394
 
326
- def has_table(cls)
327
- (cls!=ActiveRecord::Base) && cls.respond_to?(:table_name) && !cls.table_name.blank?
395
+ def dbmodel_options
396
+ {
397
+ :all_models=>parameters.check_all_models,
398
+ :dynamic_models=>parameters.check_dynamic_models,
399
+ :exclude_sti_models=>true,
400
+ :include_files=>true,
401
+ :only_app_files=>!parameters.update_vendor_models,
402
+ :only_app_tree_files=>parameters.update_vendor_models
403
+ }
328
404
  end
329
405
 
330
406
  def map_column_to_field_declaration(column)
data/modalfields.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "modalfields"
8
- s.version = "1.2.2"
8
+ s.version = "1.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Javier Goizueta"]
12
- s.date = "2012-09-05"
12
+ s.date = "2012-09-06"
13
13
  s.description = "ModelFields is a Rails plugin that adds fields declarations to your models."
14
14
  s.email = "jgoizueta@gmail.com"
15
15
  s.extra_rdoc_files = [
@@ -64,6 +64,7 @@ Gem::Specification.new do |s|
64
64
  s.specification_version = 3
65
65
 
66
66
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
67
+ s.add_runtime_dependency(%q<modalsettings>, [">= 0"])
67
68
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3.5"])
68
69
  s.add_runtime_dependency(%q<activerecord>, [">= 2.3.5"])
69
70
  s.add_development_dependency(%q<shoulda>, [">= 0"])
@@ -74,6 +75,7 @@ Gem::Specification.new do |s|
74
75
  s.add_development_dependency(%q<pg>, [">= 0"])
75
76
  s.add_runtime_dependency(%q<rails>, [">= 2.3.0"])
76
77
  else
78
+ s.add_dependency(%q<modalsettings>, [">= 0"])
77
79
  s.add_dependency(%q<activesupport>, [">= 2.3.5"])
78
80
  s.add_dependency(%q<activerecord>, [">= 2.3.5"])
79
81
  s.add_dependency(%q<shoulda>, [">= 0"])
@@ -85,6 +87,7 @@ Gem::Specification.new do |s|
85
87
  s.add_dependency(%q<rails>, [">= 2.3.0"])
86
88
  end
87
89
  else
90
+ s.add_dependency(%q<modalsettings>, [">= 0"])
88
91
  s.add_dependency(%q<activesupport>, [">= 2.3.5"])
89
92
  s.add_dependency(%q<activerecord>, [">= 2.3.5"])
90
93
  s.add_dependency(%q<shoulda>, [">= 0"])
data/test/helper.rb CHANGED
@@ -29,7 +29,7 @@ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) # + '/../../../..'
29
29
 
30
30
  module Rails
31
31
  def self.root
32
- ENV['RAILS_ROOT']
32
+ Pathname(ENV['RAILS_ROOT'])
33
33
  end
34
34
  def version
35
35
  ActiveRecord::VERSION::STRING
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modalfields
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,24 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-05 00:00:00.000000000 Z
12
+ date: 2012-09-06 00:00:00.000000000 Z
13
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: modalsettings
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
14
30
  - !ruby/object:Gem::Dependency
15
31
  name: activesupport
16
32
  requirement: !ruby/object:Gem::Requirement
@@ -214,7 +230,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
214
230
  version: '0'
215
231
  segments:
216
232
  - 0
217
- hash: 1098194892061652516
233
+ hash: -375049972268577712
218
234
  required_rubygems_version: !ruby/object:Gem::Requirement
219
235
  none: false
220
236
  requirements: