2016-10-11 20:44:41 +00:00
|
|
|
# frozen_string_literal: true
|
2011-02-05 14:22:08 +00:00
|
|
|
module Kaminari
|
|
|
|
module Generators
|
2011-02-09 10:24:20 +00:00
|
|
|
class ViewsGenerator < Rails::Generators::NamedBase
|
2011-02-07 02:32:27 +00:00
|
|
|
source_root File.expand_path('../../../../app/views/kaminari', __FILE__)
|
|
|
|
|
2016-11-22 13:16:57 +00:00
|
|
|
class_option :template_engine, type: :string, aliases: '-e', desc: 'Template engine for the views. Available options are "erb", "haml", and "slim".'
|
|
|
|
class_option :views_prefix, type: :string, desc: 'Prefix for path to put views in.'
|
2011-02-05 14:22:08 +00:00
|
|
|
|
2011-02-10 23:22:59 +00:00
|
|
|
def self.banner #:nodoc:
|
2011-02-09 10:24:20 +00:00
|
|
|
<<-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:
|
|
|
|
|
2011-02-12 17:32:58 +00:00
|
|
|
- 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")}
|
2011-02-09 10:24:20 +00:00
|
|
|
BANNER
|
|
|
|
end
|
|
|
|
|
|
|
|
desc ''
|
2011-02-12 17:32:58 +00:00
|
|
|
def copy_or_fetch #:nodoc:
|
|
|
|
return copy_default_views if file_name == 'default'
|
|
|
|
|
2016-10-11 22:48:25 +00:00
|
|
|
if (theme = self.class.themes.detect {|t| t.name == file_name})
|
2014-09-11 01:07:17 +00:00
|
|
|
if download_templates(theme).empty?
|
2016-10-11 22:45:06 +00:00
|
|
|
say "template_engine: #{template_engine} is not available for theme: #{file_name}"
|
2014-09-11 01:07:17 +00:00
|
|
|
end
|
2011-02-12 17:32:58 +00:00
|
|
|
else
|
2016-10-11 22:45:06 +00:00
|
|
|
say "no such theme: #{file_name}\n available themes: #{self.class.themes.map(&:name).join ', '}"
|
2011-02-07 02:32:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2011-02-12 17:32:58 +00:00
|
|
|
def self.themes
|
2016-10-11 22:23:12 +00:00
|
|
|
@themes ||= GitHubApiHelper.get_files_in_master.group_by {|fn, _| fn[0...(fn.index('/') || 0)]}.delete_if {|fn, _| fn.blank?}.map do |name, files|
|
|
|
|
Theme.new name, files
|
2011-02-12 17:32:58 +00:00
|
|
|
end
|
2016-10-11 22:23:12 +00:00
|
|
|
rescue SocketError
|
|
|
|
[]
|
2011-02-12 17:32:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def download_templates(theme)
|
|
|
|
theme.templates_for(template_engine).each do |template|
|
|
|
|
say " downloading #{template.name} from kaminari_themes..."
|
2015-03-19 11:56:49 +00:00
|
|
|
create_file view_path_for(template.name), GitHubApiHelper.get_content_for("#{theme.name}/#{template.name}")
|
2011-02-12 17:32:58 +00:00
|
|
|
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|
|
2015-03-19 11:56:49 +00:00
|
|
|
copy_file f, view_path_for(f)
|
2011-02-12 17:32:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-19 11:56:49 +00:00
|
|
|
def view_path_for(file)
|
|
|
|
['app', 'views', views_prefix, 'kaminari', File.basename(file)].compact.join('/')
|
|
|
|
end
|
|
|
|
|
|
|
|
def views_prefix
|
|
|
|
options[:views_prefix].try(:to_s)
|
|
|
|
end
|
|
|
|
|
2011-02-12 17:32:58 +00:00
|
|
|
def template_engine
|
|
|
|
options[:template_engine].try(:to_s).try(:downcase) || 'erb'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Template = Struct.new(:name, :sha) do
|
2011-04-17 16:47:45 +00:00
|
|
|
def description?
|
|
|
|
name == 'DESCRIPTION'
|
|
|
|
end
|
|
|
|
|
|
|
|
def view?
|
|
|
|
name =~ /^app\/views\//
|
2011-02-12 17:32:58 +00:00
|
|
|
end
|
|
|
|
|
2011-04-17 16:47:45 +00:00
|
|
|
def engine #:nodoc:
|
2016-10-11 22:46:54 +00:00
|
|
|
File.extname(name).sub(/^\./, '')
|
2011-02-12 17:32:58 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Theme
|
|
|
|
attr_accessor :name
|
|
|
|
def initialize(name, templates) #:nodoc:
|
2011-04-17 16:47:45 +00:00
|
|
|
@name, @templates = name, templates.map {|fn, sha| Template.new fn.sub(/^#{name}\//, ''), sha}
|
2011-02-12 17:32:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def description #:nodoc:
|
2011-04-17 16:47:45 +00:00
|
|
|
file = @templates.detect(&:description?)
|
2011-02-12 17:32:58 +00:00
|
|
|
return "#{' ' * 12}#{name}" unless file
|
2012-09-04 15:43:56 +00:00
|
|
|
GitHubApiHelper.get_content_for("#{@name}/#{file.name}").chomp.gsub(/^/, ' ' * 12)
|
2011-02-09 10:24:20 +00:00
|
|
|
end
|
|
|
|
|
2011-02-12 17:32:58 +00:00
|
|
|
def templates_for(template_engine) #:nodoc:
|
2014-11-20 23:44:38 +00:00
|
|
|
@templates.select {|t| t.engine == template_engine }
|
2011-02-05 14:22:08 +00:00
|
|
|
end
|
|
|
|
end
|
2012-09-04 15:43:56 +00:00
|
|
|
|
|
|
|
module GitHubApiHelper
|
|
|
|
def get_files_in_master
|
|
|
|
master_tree_sha = open('https://api.github.com/repos/amatsuda/kaminari_themes/git/refs/heads/master') do |json|
|
2014-03-04 18:19:58 +00:00
|
|
|
ActiveSupport::JSON.decode(json.read)['object']['sha']
|
2012-09-04 15:43:56 +00:00
|
|
|
end
|
|
|
|
open('https://api.github.com/repos/amatsuda/kaminari_themes/git/trees/' + master_tree_sha + '?recursive=1') do |json|
|
2014-03-04 18:19:58 +00:00
|
|
|
blobs = ActiveSupport::JSON.decode(json.read)['tree'].find_all {|i| i['type'] == 'blob' }
|
2012-09-04 15:43:56 +00:00
|
|
|
blobs.map do |blob|
|
|
|
|
[blob['path'], blob['sha']]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :get_files_in_master
|
|
|
|
|
|
|
|
def get_content_for(path)
|
|
|
|
open('https://api.github.com/repos/amatsuda/kaminari_themes/contents/' + path) do |json|
|
2014-03-04 18:19:58 +00:00
|
|
|
Base64.decode64(ActiveSupport::JSON.decode(json.read)['content'])
|
2012-09-04 15:43:56 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
module_function :get_content_for
|
|
|
|
end
|
2011-02-05 14:22:08 +00:00
|
|
|
end
|
|
|
|
end
|