Class: GettextI18nRails::ModelAttributesFinder
- Inherits:
-
Object
- Object
- GettextI18nRails::ModelAttributesFinder
- Defined in:
- lib/gettext_i18n_rails/model_attributes_finder.rb
Instance Method Summary collapse
-
#find(options) ⇒ Object
options: :ignore_tables => [‘cars’,/_settings$/,…] :ignore_columns => [‘id’,/_id$/,…] current connection —> href="'model_name','type'">cars’=>,….
- #ignored?(name, patterns) ⇒ Boolean
-
#initialize ⇒ ModelAttributesFinder
constructor
A new instance of ModelAttributesFinder.
-
#model_attributes(model, ignored_tables, ignored_cols) ⇒ Object
Rails < 3.0 doesn’t have DescendantsTracker.
- #models ⇒ Object
-
#subclass_method ⇒ Object
Rails 7.0 has deprecated direct_descendants in favor of subclasses.
Constructor Details
#initialize ⇒ ModelAttributesFinder
Returns a new instance of ModelAttributesFinder.
44 45 46 47 |
# File 'lib/gettext_i18n_rails/model_attributes_finder.rb', line 44 def initialize connection = ::ActiveRecord::Base.connection @existing_tables = (Rails::VERSION::MAJOR >= 5 ? connection.data_sources : connection.tables) end |
Instance Method Details
#find(options) ⇒ Object
options:
:ignore_tables => ['cars',/_settings$/,...]
:ignore_columns => ['id',/_id$/,...]
current connection —> href="'model_name','type'">cars’=>,…
35 36 37 38 39 40 41 42 |
# File 'lib/gettext_i18n_rails/model_attributes_finder.rb', line 35 def find() found = ActiveSupport::OrderedHash.new([]) models.each do |model| attributes = model_attributes(model, [:ignore_tables], [:ignore_columns]) found[model] = attributes.sort if attributes.any? end found end |
#ignored?(name, patterns) ⇒ Boolean
99 100 101 102 |
# File 'lib/gettext_i18n_rails/model_attributes_finder.rb', line 99 def ignored?(name,patterns) return false unless patterns patterns.detect{|p|p.to_s==name.to_s or (p.is_a?(Regexp) and name=~p)} end |
#model_attributes(model, ignored_tables, ignored_cols) ⇒ Object
Rails < 3.0 doesn’t have DescendantsTracker. Instead of iterating over ObjectSpace (slow) the decision was made NOT to support class hierarchies with abstract base classes in Rails 2.x
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/gettext_i18n_rails/model_attributes_finder.rb', line 63 def model_attributes(model, ignored_tables, ignored_cols) return [] if model.abstract_class? && Rails::VERSION::MAJOR < 3 if model.abstract_class? model.send(subclass_method).reject {|m| ignored?(m.table_name, ignored_tables)}.inject([]) do |attrs, m| attrs.push(model_attributes(m, ignored_tables, ignored_cols)).flatten.uniq end elsif !ignored?(model.table_name, ignored_tables) && @existing_tables.include?(model.table_name) model.columns.reject { |c| ignored?(c.name, ignored_cols) }.collect { |c| c.name } else [] end end |
#models ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/gettext_i18n_rails/model_attributes_finder.rb', line 77 def models if Rails::VERSION::MAJOR >= 3 # Ensure autoloaders are set up before we attempt to eager load! Rails.application.autoloaders.each(&:setup) if Rails.application.respond_to?(:autoloaders) Rails.application.eager_load! # make sure that all models are loaded so that direct_descendants works descendants = ::ActiveRecord::Base.send(subclass_method) # In rails 5+ user models are supposed to inherit from ApplicationRecord if defined?(::ApplicationRecord) descendants += ApplicationRecord.send(subclass_method) descendants.delete ApplicationRecord end descendants else ::ActiveRecord::Base.connection.tables \ .map { |t| table_name_to_namespaced_model(t) } \ .compact \ .collect { |c| c.superclass.abstract_class? ? c.superclass : c } end.uniq.sort_by(&:name) end |
#subclass_method ⇒ Object
Rails 7.0 has deprecated direct_descendants in favor of subclasses. It was removed completely in Rails 7.1. This will maintain backwards compatibility with Rails 3.0 - 6.1
52 53 54 55 56 57 58 |
# File 'lib/gettext_i18n_rails/model_attributes_finder.rb', line 52 def subclass_method if Rails::VERSION::MAJOR < 7 :direct_descendants else :subclasses end end |