Class: GettextI18nRails::ModelAttributesFinder

### Home Classes Methods

### In Files

- lib/gettext_i18n_rails/model_attributes_finder.rb

### Parent

Object

### Methods

- ::new
- #find
- #ignored?
- #model_attributes
- #models

### Class/Module Index

Quicksearch

- GettextI18nRails
- GettextI18nRails::Backend
- GettextI18nRails::BaseParser
- GettextI18nRails::GettextHooks
- GettextI18nRails::HamlParser
- GettextI18nRails::HtmlSafeTranslations
- GettextI18nRails::ModelAttributesFinder
- GettextI18nRails::Railtie
- GettextI18nRails::SlimParser
- RubyGettextExtractor
- RubyGettextExtractor::Extractor18
- RubyGettextExtractor::Extractor19
- RubyGettextExtractor::ExtractorMethods
- ActionController
- ActionController::Base
- ActiveModel
- ActiveModel::Translation
- ActiveRecord
- ActiveRecord::Base
- I18n
- I18n::Config
- Object
- String

No matching classes.

# GettextI18nRails::ModelAttributesFinder

### Public Class Methods

new() click to toggle source

    # File lib/gettext_i18n_rails/model_attributes_finder.rb, line 44
    def initialize
      @existing_tables = ActiveRecord::Base.connection.tables
    end

### Public Instance Methods

find(options) click to toggle source

options:

    :ignore_tables => ['cars',/_settings$/,...]
    :ignore_columns => ['id',/_id$/,...]

current connection ---> {'cars'=>,...}

    # File lib/gettext_i18n_rails/model_attributes_finder.rb, line 35
    def find(options)
      found = ActiveSupport::OrderedHash.new([])
      models.each do |model|
        attributes = model_attributes(model, options[:ignore_tables], options[:ignore_columns])
        found[model] = attributes.sort if attributes.any?
      end
      found
    end

ignored?(name,patterns) click to toggle source

    # File lib/gettext_i18n_rails/model_attributes_finder.rb, line 77
    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) click to toggle source

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

    # File lib/gettext_i18n_rails/model_attributes_finder.rb, line 51
    def model_attributes(model, ignored_tables, ignored_cols)
      return [] if model.abstract_class? && Rails::VERSION::MAJOR < 3

      if model.abstract_class?
        model.direct_descendants.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() click to toggle source

    # File lib/gettext_i18n_rails/model_attributes_finder.rb, line 65
    def models
      if Rails::VERSION::MAJOR > 2
        Rails.application.eager_load! # make sure that all models are loaded so that direct_descendants works
        ::ActiveRecord::Base.direct_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

[Validate]

Generated with the Darkfish Rdoc Generator 2.