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

Override presenters #1790

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
184 changes: 73 additions & 111 deletions core/lib/refinery/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,123 +4,60 @@ module Refinery
class CLI < Thor
include Thor::Actions

KINDS = {
'view' => {
'glob' => '*.{erb,builder}',
'dir' => 'views',
'desc' => 'view template',
},
'controller' => {
'glob' => '*.rb',
'dir' => 'controllers',
'desc' => 'controller',
},
'model' => {
'glob' => '*.rb',
'dir' => 'models',
'desc' => 'model',
},
'presenter' => {
'glob' => '*.rb',
'dir' => 'presenters',
'desc' => 'presenter',
},
'javascript' => {
'glob' => '*.js{,.*}',
'dir' => 'assets/javascripts',
'desc' => 'javascript',
},
'stylesheet' => {
'glob' => '*.css.scss',
'dir' => 'assets/stylesheets',
'desc' => 'stylesheet',
},
}

desc "override", "copies files from any Refinery extension that you are using into your application"
def override(env)
if (view = env["view"]).present?
pattern = "{refinery#{File::SEPARATOR},}#{view.split("/").join(File::SEPARATOR)}*.{erb,builder}"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "views", pattern).to_s}

# copy in the matches
if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
matches.each do |match|
dir = match.split("/app/views/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together

destination_dir = Rails.root.join("app", "views", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))

puts "Copied view template file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any view template files in any extensions like #{view}"
end
elsif (controller = env["controller"]).present?
pattern = "{refinery#{File::SEPARATOR},}#{controller.split("/").join(File::SEPARATOR)}*.rb"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "controllers", pattern).to_s}

# copy in the matches
if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
matches.each do |match|
dir = match.split("/app/controllers/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together

destination_dir = Rails.root.join("app", "controllers", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))

puts "Copied controller file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any controller files in any extensions like #{controller}"
end
elsif (model = env["model"]).present?
pattern = "{refinery#{File::SEPARATOR},}#{model.split("/").join(File::SEPARATOR)}*.rb"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "models", pattern).to_s}

# copy in the matches
if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
matches.each do |match|
dir = match.split("/app/models/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together

destination_dir = Rails.root.join("app", "models", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))

puts "Copied model file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any model files in any extensions like #{model}"
end
elsif (javascripts = env["javascript"]).present?
pattern = "#{javascripts.split("/").join(File::SEPARATOR)}*.js{,.*}"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "assets", "javascripts", pattern).to_s}

# copy in the matches
if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
matches.each do |match|
dir = match.split("/app/assets/javascripts/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together

destination_dir = Rails.root.join("app", "assets", "javascripts", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))

puts "Copied javascript file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any javascript files in any extensions like #{javascripts}"
for kind in KINDS.keys
if (which = env[kind]).present?
return _override(kind, which)
end
elsif (stylesheets = env["stylesheet"]).present?
pattern = "#{stylesheets.split("/").join(File::SEPARATOR)}*.css.scss"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", "assets", "stylesheets", pattern).to_s}

# copy in the matches
if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
matches.each do |match|
dir = match.split("/app/assets/stylesheets/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together

destination_dir = Rails.root.join("app", "assets", "stylesheets", dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))
end

puts "Copied stylesheet file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any stylesheet files in any extensions like #{stylesheets}"
end
else
puts "You didn't specify anything to override. Here are some examples:"
{
:view => ['pages/home', 'refinery/pages/home', '**/*menu', '_menu_branch'],
:javascript => %w(admin refinery/site_bar),
:stylesheet => %w(home refinery/site_bar),
:controller => %w(pages),
:model => %w(page refinery/page)
}.each do |type, examples|
examples.each do |example|
puts "rake refinery:override #{type}=#{example}"
end
puts "You didn't specify anything to override. Here are some examples:"
{
:view => ['pages/home', 'refinery/pages/home', '**/*menu', '_menu_branch'],
:javascript => %w(admin refinery/site_bar),
:stylesheet => %w(home refinery/site_bar),
:controller => %w(pages),
:model => %w(page refinery/page),
:presenter => %w(refinery/page_presenter)
}.each do |type, examples|
examples.each do |example|
puts "rake refinery:override #{type}=#{example}"
end
end

end

desc "uncrudify", "shows you the code that your controller using crudify is running for a given action"
Expand Down Expand Up @@ -158,5 +95,30 @@ def uncrudify(controller, action)
puts crud_method
end
end


private

def _override(kind, which)
pattern = "{refinery#{File::SEPARATOR},}#{which.split("/").join(File::SEPARATOR)}#{KINDS[kind]['glob']}"
looking_for = ::Refinery::Plugins.registered.pathnames.map{|p| p.join("app", KINDS[kind]['dir'], pattern).to_s}

# copy in the matches
if (matches = looking_for.map{|d| Dir[d]}.flatten.compact.uniq).any?
matches.each do |match|
dir = match.split("/app/#{KINDS[kind]['dir']}/").last.split('/')
file = dir.pop # get rid of the file.
dir = dir.join(File::SEPARATOR) # join directory back together

destination_dir = Rails.root.join("app", KINDS[kind]['dir'], dir)
FileUtils.mkdir_p(destination_dir)
FileUtils.cp match, (destination = File.join(destination_dir, file))

puts "Copied #{KINDS[kind]['desc']} file to #{destination.gsub("#{Rails.root.to_s}#{File::SEPARATOR}", '')}"
end
else
puts "Couldn't match any #{KINDS[kind]['desc']} files in any extensions like #{which}"
end
end
end
end