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

Fixes #1

Open
wants to merge 3 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: 2 additions & 0 deletions lib/arel_converter.rb
Expand Up @@ -10,13 +10,15 @@
require File.join('arel_converter', 'formatter')
require File.join('arel_converter', 'active_record_finder')
require File.join('arel_converter', 'scope')
require File.join('arel_converter', 'default_scope')
require File.join('arel_converter', 'association')
require File.join('arel_converter', 'replacement')

# Translators
require File.join('arel_converter', 'translators', 'base')
require File.join('arel_converter', 'translators', 'options')
require File.join('arel_converter', 'translators', 'scope')
require File.join('arel_converter', 'translators', 'default_scope')
require File.join('arel_converter', 'translators', 'finder')
require File.join('arel_converter', 'translators', 'association')

Expand Down
2 changes: 1 addition & 1 deletion lib/arel_converter/active_record_finder.rb
Expand Up @@ -3,7 +3,7 @@ class ActiveRecordFinder < Base

def grep_matches_in_file(file)
raw_ar_finders = ''
["find(:all", "find(:first", "find.*:conditions =>", '\.all(', '\.first('].each do |v|
["find(:all", "find(:first", "find.*:conditions =>"].each do |v|
raw_ar_finders += `grep -hr '#{v}' #{file}`
end

Expand Down
2 changes: 1 addition & 1 deletion lib/arel_converter/association.rb
Expand Up @@ -2,7 +2,7 @@ module ArelConverter
class Association < Base

def grep_matches_in_file(file)
raw_named_scopes = `grep -hr "^\s*has_\\|belongs_to" #{file}`
raw_named_scopes = `grep -hr "^\s*has_one\\|has_many\\|has_and_belongs_to_many\\|belongs_to" #{file}`
raw_named_scopes.split("\n")
end

Expand Down
7 changes: 6 additions & 1 deletion lib/arel_converter/base.rb
Expand Up @@ -13,7 +13,12 @@ def run!
end

def parse_directory(path)
Dir[File.join(path, '**/*.rb')].each do |file|
[
Dir[File.join(path, '**/*.haml')],
Dir[File.join(path, '**/*.erb')],
Dir[File.join(path, '**/*.slim')],
Dir[File.join(path, '**/*.rb')]
].flatten.each do |file|
begin
parse_file(file)
rescue => e
Expand Down
1 change: 1 addition & 0 deletions lib/arel_converter/command.rb
Expand Up @@ -22,6 +22,7 @@ def run!
if @translators.include?('scope')
puts "\n== Checking Scopes"
ArelConverter::Scope.new(options[:path], options).run!
ArelConverter::DefaultScope.new(options[:path], options).run!
end

if @translators.include?('finder')
Expand Down
22 changes: 22 additions & 0 deletions lib/arel_converter/default_scope.rb
@@ -0,0 +1,22 @@
module ArelConverter
class DefaultScope < Base

def grep_matches_in_file(file)
raw_named_scopes = `grep -h -r "^\s*default_scope" #{file}`
raw_named_scopes.split("\n")
end

def process_line(line)
new_scope = ArelConverter::Translator::DefaultScope.translate(line)
new_scope.gsub(/default_scope\((.*)\)$/, 'default_scope \1')
end

def verify_line(line)
parser = RubyParser.new
sexp = parser.process(line)
sexp[0] == :call && sexp[2] == :default_scope
end

end

end
20 changes: 18 additions & 2 deletions lib/arel_converter/translators/base.rb
Expand Up @@ -2,13 +2,25 @@ module ArelConverter
module Translator
class Base < Ruby2Ruby

attr_accessor :leading_whitespace

LINE_LENGTH = 1_000

def self.translate(klass_or_str, method = nil)
sexp = klass_or_str.is_a?(String) ? self.parse(klass_or_str) : klass_or_str
leading_whitespace = nil
if klass_or_str.is_a?(String)
match = klass_or_str.match(/^(\s+)/)
if match[0]
leading_whitespace = match[0]
end
sexp = self.parse(klass_or_str)
else
sexp = klass_or_str
end
processor = self.new
processor.leading_whitespace = leading_whitespace
source = processor.process(sexp)
processor.post_processing(source)
processor.retain_leading_whitespace(processor.post_processing(source))
end

def self.parse(code)
Expand All @@ -23,6 +35,10 @@ def post_processing(source)
source
end

def retain_leading_whitespace(source)
leading_whitespace ? source.prepend(leading_whitespace) : source
end

def format_for_hash(key, value)
key =~ /\A:/ ? "#{key.sub(':','')}: #{value}" : "#{key} => #{value}"
end
Expand Down
24 changes: 24 additions & 0 deletions lib/arel_converter/translators/default_scope.rb
@@ -0,0 +1,24 @@
module ArelConverter
module Translator
class DefaultScope < Base

def process_call(exp)
@options = Options.translate(exp.pop) if exp[1] == :default_scope
super
end

def post_processing(new_scope)
new_scope.gsub!(/default_scope\((.*)\)$/, 'default_scope \1')
new_scope += format_options(@options)
end

protected

def format_options(options)
return if options.nil? || options.empty?
" { #{options.strip} }"
end

end
end
end
2 changes: 1 addition & 1 deletion lib/arel_converter/translators/options.rb
Expand Up @@ -48,7 +48,7 @@ def hash_to_arel(lhs, rhs)
when 'none', 'reverse_order'
return key
end
rhs = rhs.gsub(/\A\[(.*)\]\z/, '\1').gsub(/\A\{(.*)\}\z/, '\1')
rhs = rhs.gsub(/\A\[(.*)\]\z/, '\1').gsub(/\A\{(.*)\}\z/, '\1').strip
"#{key}(#{rhs})"
end

Expand Down