Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModelInspector: Subclasses #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Expand Up @@ -63,7 +63,7 @@ GEM
activesupport (= 4.0.4)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.2.2)
rake (10.3.1)
rspec-core (2.14.8)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
Expand Down
64 changes: 56 additions & 8 deletions lib/gettext_simple_rails/model_inspector.rb
@@ -1,14 +1,62 @@
class GettextSimpleRails::ModelInspector
def self.model_classes
clazzes = []

def self.model_classes(&blk)
::Rails.application.eager_load!
@scanned = {}

constants = Module.constants + Object.constants + Kernel.constants
constants.each do |constant_name|
model_classes_scan(Kernel, constant_name, &blk)
end
end

# Tells if a class is an ActiveRecord model and handles errors.
def self.active_record_status(clazz)
begin
return :yes if clazz < ActiveRecord::Base
rescue NoMethodError, ArgumentError
return :skip
end

return :no
end

def self.model_classes_scan(mod, constant_name, &blk)
return if !mod.const_defined?(constant_name)

begin
clazz = mod.const_get(constant_name)
rescue
return
end

result = active_record_status(clazz)
if result == :yes
blk.call(::GettextSimpleRails::ModelInspector.new(clazz))
elsif result == :skip
return
end

::Object.constants.each do |clazz|
clazz = clazz.to_s.constantize
next unless clazz.class == Class
next unless clazz < ActiveRecord::Base
yield ::GettextSimpleRails::ModelInspector.new(clazz)
clazz.constants.each do |class_sym|
# Supresses a bit of the errors.½
next if class_sym == :Fixtures || clazz.autoload?(class_sym)

begin
class_current = clazz.const_get(class_sym)
rescue
next
rescue RuntimeError, LoadError
next
end

next if !class_current.is_a?(Class) && !class_current.is_a?(Module)

if @scanned[class_current]
next
else
@scanned[class_current] = true
end

model_classes_scan(clazz, class_sym, &blk)
end
end

Expand Down
7 changes: 7 additions & 0 deletions lib/tasks/gettext_simple_rails_tasks.rake
Expand Up @@ -29,6 +29,13 @@ namespace :gettext_simple_rails do

task "generate_model_translation_files" => :environment do
GettextSimpleRails::ModelInspector.model_classes do |inspector|
puts "Class: #{inspector.clazz.name}"

if inspector.clazz.name.to_s.match(/::Translation$/)
# puts "Skipping because translation: #{inspector.clazz}"
next
end

translation_path = "#{GettextSimpleRails.translation_dir}/models/#{inspector.snake_name}_model_translations.rb"
FileUtils.mkdir_p(File.dirname(translation_path)) unless File.exists?(File.dirname(translation_path))

Expand Down
31 changes: 31 additions & 0 deletions spec/model_inspector_spec.rb
@@ -0,0 +1,31 @@
require "spec_helper"

# Some subclasses the modelinspector should find later in the specs.
class SomeClass
class SomeSubClass < ActiveRecord::Base
end
end

module SomeModule
class SomeSubClassOfModule < ActiveRecord::Base
end
end


describe GettextSimpleRails::ModelInspector do
it "should register subclasses" do
found_sub_class = false
found_sub_class_of_module = false

GettextSimpleRails::ModelInspector.model_classes do |inspector|
if inspector.clazz.name == "SomeClass::SomeSubClass"
found_sub_class = true
elsif inspector.clazz.name == "SomeModule::SomeSubClassOfModule"
found_sub_class_of_module = true
end
end

found_sub_class.should eq true
found_sub_class_of_module.should eq true
end
end