class MasterviewController < ApplicationController def index redirect_to :action => :list end def list template_specs, content_hash = MasterView::TemplateSpec.scan @template_specs_sorted = template_specs.sort smart_render 'masterview/admin/list' end def rebuild_all files_rebuilt = [] MasterView::TemplateSpec.scan do |template_spec, content_hash| if template_spec.status == MasterView::TemplateSpec::Status::ImportsOutdated && template_spec.rebuild_template(content_hash, :write_to_file => true) files_rebuilt << template_spec.path end end unless files_rebuilt.empty? flash[:notice] = files_rebuilt.join(', ')+' were updated' end redirect_to :action => :list end def rebuild path = params[:id] template_specs, content_hash = MasterView::TemplateSpec.scan template_spec = template_specs[path] raise 'Template '+path+' not found' unless template_spec if template_spec.rebuild_template(content_hash, :write_to_file => true) flash[:notice] = 'File '+path+' updated' else flash[:notice] = 'Identical content - no update needed for '+path end redirect_to :action => :list end def create if @request.post? action_to_create = params[:action_name] short_name = File.basename(params[:id]) src_file = File.join('app/views', MasterView::TemplateSrcRelativePath, short_name) empty_file_path = find_path('app/views/masterview/admin/empty.rhtml') empty_insert_erb = File.readlines(empty_file_path).join dst_file = MasterView::TemplateSpec.create_empty_shell_for_action(src_file, action_to_create, empty_insert_erb, :write_to_file => true) flash[:notice] = dst_file+' was created' redirect_to :action => :list else smart_render 'masterview/admin/create' end end private # checks app path first for views and files, then falls back to files in MV def find_path(path) local_path = File.join(RAILS_ROOT, path) mv_path = File.join(File.dirname(__FILE__), '../..', path) working_path = (File.exist?(local_path)) ? local_path : mv_path end # render local template file if exists otherwise render file from mv def smart_render(short_path) local_path = File.join(RAILS_ROOT, 'app/views', short_path)+'.rhtml' if File.exist?(local_path) render :template => short_path else mv_path = File.join(File.dirname(__FILE__), '../../app/views', short_path)+'.rhtml' MasterView::Log.debug { 'mv_path='+mv_path } render :file => mv_path end end end