1
0
Fork 0
mirror of https://github.com/kaminari/kaminari.git synced 2022-11-09 13:44:37 -05:00
kaminari--kaminari/lib/generators/kaminari/views_generator.rb
2011-02-13 06:03:15 +09:00

99 lines
3.2 KiB
Ruby

module Kaminari
module Generators
SHOW_API = 'http://github.com/api/v2/json/blob/show/amatsuda/kaminari_themes'
ALL_API = 'http://github.com/api/v2/json/blob/all/amatsuda/kaminari_themes/master'
class ViewsGenerator < Rails::Generators::NamedBase
source_root File.expand_path('../../../../app/views/kaminari', __FILE__)
class_option :template_engine, :type => :string, :aliases => '-e', :desc => 'Template engine for the views. Available options are "erb" and "haml".'
def self.banner #:nodoc:
<<-BANNER.chomp
rails g kaminari:views THEME [options]
Copies all paginator partial templates to your application.
You can choose a template THEME by specifying one from the list below:
- default
The default one.
This one is used internally while you don't override the partials.
#{themes.map {|t| " - #{t.name}\n#{t.description}"}.join("\n")}
BANNER
end
desc ''
def copy_or_fetch #:nodoc:
return copy_default_views if file_name == 'default'
themes = self.class.themes
if theme = themes.detect {|t| t.name == file_name}
download_templates theme
else
say %Q[no such theme: #{file_name}\n avaliable themes: #{themes.map(&:name).join ", "}]
end
end
private
def self.themes
begin
@themes ||= open ALL_API do |json|
# open File.join File.dirname(__FILE__), '../../../spec/generators/sample.json' do |json|
files = ActiveSupport::JSON.decode(json)['blobs']
hash = files.group_by {|fn, _| fn[0...(fn.index('/') || 0)]}.delete_if {|fn, _| fn.blank?}
hash.map do |name, files|
Theme.new name, files
end
end
rescue SocketError
[]
end
end
def download_templates(theme)
theme.templates_for(template_engine).each do |template|
say " downloading #{template.name} from kaminari_themes..."
get "#{SHOW_API}/#{template.sha}", "app/views/kaminari/#{template.basename}"
end
end
def copy_default_views
filename_pattern = File.join self.class.source_root, "*.html.#{template_engine}"
Dir.glob(filename_pattern).map {|f| File.basename f}.each do |f|
copy_file f, "app/views/kaminari/#{f}"
end
end
def template_engine
options[:template_engine].try(:to_s).try(:downcase) || 'erb'
end
end
Template = Struct.new(:name, :sha) do
def engine #:nodoc:
File.extname(name).sub /^\./, ''
end
def basename #:nodoc:
File.basename name
end
end
class Theme
attr_accessor :name
def initialize(name, templates) #:nodoc:
@name, @templates = name, templates.map {|f| Template.new *f}
end
def description #:nodoc:
file = @templates.detect {|t| t.name == "#{name}/DESCRIPTION"}
return "#{' ' * 12}#{name}" unless file
open("#{SHOW_API}/#{file.sha}").read.chomp.gsub /^/, ' ' * 12
end
def templates_for(template_engine) #:nodoc:
@templates.select {|t| t.engine == template_engine}
end
end
end
end