Plugin = Fanforce::Plugin PluginWorker = Fanforce::PluginWorker ######################################################################################################################## class Fanforce::Plugin::Sinatra < Sinatra::Base require 'bugsnag' require 'rack/contrib' require_relative 'error_handling' require_relative 'helpers/assets' require_relative 'helpers/ractive' require_relative 'helpers/json' require_relative 'helpers/fanforce' require_relative 'helpers/css_frameworks' use ::Rack::JSONR, :method_override => :all use ::Rack::PostBodyContentTypeParser use ::Bugsnag::Rack helpers AssetHelpers helpers FanforceHelpers helpers JSONHelpers helpers RactiveHelpers @@static_files = {} def self.any(url,&block) get(url, &block) put(url, &block) post(url, &block) delete(url, &block) end def self.require_controllers require "#{Fanforce::Plugin.config.factory_dir}/controllers/base_controller" Dir["#{Fanforce::Plugin.config.base_dir}/controllers/*_controller.rb"].each {|file| require file } end def self.setup_catchall_routes_from_folder(path) path = ('views/' + path.to_s.gsub(/^views\/?/, '')).gsub(/\/$/, '') Dir["#{Fanforce::Plugin.config.base_dir}/#{path}/**/*.{html,haml}"].each do |file| create_basic_route($1) if (file =~ /\/views\/(.+)\.haml/) || (file =~ /\/views\/(.+\.html)/) end end def self.setup_catchall_routes_from_views_folder setup_catchall_routes_from_folder(:views) end def self.create_basic_route(path) get "/#{path}" do page(path) end end def self.static(url, *args, &block) @@static_files[url] = {args: args, block: block} get(url, &block) end def factory_directive(page) factory_dir = Pathname.new(Fanforce::Plugin.config.factory_dir) plugin_views_dir = Pathname.new(settings.views) haml :"#{factory_dir.relative_path_from(plugin_views_dir).to_s}/directive_views/#{page}" end def partial(path, options={}) options[:css_framework] = params[:css_framework] if params[:css_framework] haml(path.to_sym, options) end def page(page, options={}) plugin_views_dir = Pathname.new(settings.views) if File.exists?("#{plugin_views_dir}/#{options[:com_dir]}/#{page}.haml") page = :"#{options[:com_dir]}/#{page}" file_type = :haml elsif File.exists?("#{plugin_views_dir}/#{options[:com_dir]}/#{page}") page = :"#{options[:com_dir]}/#{page}" file_type = :html elsif File.exists?("#{plugin_views_dir}/#{page}.haml") file_type = :haml else file_type = :html end if file_type == :haml options[:layout] = options[:layout] ? :"../layouts/#{options[:layout]}" : false locals = {page_classes: page.to_s.split('/')} haml(page.to_sym, options, locals) else html("#{plugin_views_dir}/#{page}") end end def html(filepath) File.read(filepath) end configure do Fanforce::Plugin.config.load_bugsnag Fanforce::Plugin.config.load_routes set :root, Fanforce::Plugin.config.base_dir set :haml, {escape_attrs: false} set :static, true set :protection, :except => [:frame_options, :json_csrf] end get '/' do "#{Fanforce::Plugin._id} is ready to go! All you need are a few routes." end end