lib/venomi/rails_admin.rb in venomi-0.0.1 vs lib/venomi/rails_admin.rb in venomi-0.0.2

- old
+ new

@@ -1,60 +1,50 @@ require 'pathname' require 'fileutils' require 'tempfile' +require 'generators/utils' include FileUtils module Venomi module RailsAdmin if defined? ::Rails @rails_root = Rails.root.to_s - @rails_admin_root = @rails_root + "/config/initializers/rails_admin.rb" + @rails_admin_root = "#{@rails_root}/config/initializers/rails_admin.rb" else - @gem_root = Pathname.new File.expand_path('../../', __FILE__) - @rails_admin_root = @gem_root.to_s + "/debug/initializers/rails_admin.rb" + # Debug + @gem_root = Pathname.new File.expand_path('../../', __FILE__).to_s + @rails_admin_root = "#{@gem_root}/debug/initializers/rails_admin.rb" end - def self.configure - unless file_include?(@rails_admin_root, "config.model Translation do") - replace(@rails_admin_root, "RailsAdmin.config do |config|", ("RailsAdmin.config do |config|\n" + @translation)) - end + class << self + include Venomi::Generators::Utils::InstanceMethods - unless file_include?(@rails_admin_root, " new do") - replace(@rails_admin_root, " new", @new) - end + def configure + if file?(@rails_admin_root) + unless file_include?(@rails_admin_root, "config.model Translation do") + replace(@rails_admin_root, "RailsAdmin.config do |config|", ("RailsAdmin.config do |config|\n" + @translation)) + end - unless file_include?(@rails_admin_root, " delete do") - replace(@rails_admin_root, " delete", @delete) - end - end + unless file_include?(@rails_admin_root, " new do") + replace(@rails_admin_root, " new", @new) + end - def self.rollback - text = File.read(@rails_admin_root) - text.gsub!(@delete, " delete\n") - text.gsub!(@new, " new\n") - text.gsub!(@translation, "") - File.open(@rails_admin_root, "w") {|file| file.puts text } - end - - def self.replace(path, pattern, new_line) - t_file = Tempfile.new('temp.rb') - File.open(path, 'r') do |f| - f.each_line do |line| - t_file.puts (line.include? pattern)? new_line : line + unless file_include?(@rails_admin_root, " delete do") + replace(@rails_admin_root, " delete", @delete) + end end end - t_file.close - FileUtils.mv(t_file.path, path) - end - def self.file_include?(path, include) - File.open(path, 'r') do |f| - f.each_line do |line| - return true if line.include? include + def rollback + if file? @rails_admin_root + text = File.read(@rails_admin_root) + text.gsub!(@delete, " delete\n") + text.gsub!(@new, " new\n") + text.gsub!(@translation, "") + File.open(@rails_admin_root, "w") {|file| file.puts text } end end - false end @new = <<-MSG new do except [:Translation]