-
-
Save arman-h/5687221 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'slim' | |
| require 'i18n' | |
| require 'mysql2' | |
| require 'sprockets' | |
| require 'sinatra/base' | |
| require 'sinatra/cookies' | |
| require 'sinatra/content_for' | |
| require 'sinatra/activerecord' | |
| require 'sinatra/partial' | |
| require 'rack-flash' | |
| class App < Sinatra::Base | |
| enable :sessions, :logging | |
| register Sinatra::Partial | |
| use Rack::MethodOverride | |
| use Rack::Flash, :accessorize => [:info, :error, :success], :sweep => true | |
| use Rack::Protection::AuthenticityToken # HTML forms now require: input name="authenticity_token" value=session[:csrf] type="hidden" | |
| set :public_folder, File.dirname(__FILE__) + '/public' | |
| set :views, File.dirname(__FILE__) + '/app/views' | |
| set :slim, :layout_engine => :slim, :layout => :'layouts/default', :pretty => true | |
| set :partial_template_engine, :slim | |
| set :session_secret, "25729f31a6bc7c57f8575db9b79ee468...." # SecureRandom.hex(128) | |
| set :cookie_options, { path: '/'} | |
| def self.sprockets | |
| project_root = File.expand_path(File.dirname(__FILE__)) | |
| assets = Sprockets::Environment.new(project_root) | |
| assets.append_path('app/js') | |
| assets.append_path('app/css') | |
| # Twitter Bootstrap... | |
| assets.append_path('lib/bootstrap/js') | |
| assets.append_path('lib/bootstrap/css') | |
| assets | |
| end | |
| helpers Sinatra::Cookies | |
| helpers Sinatra::ContentFor | |
| helpers do | |
| def t(*args) | |
| ::I18n::t(*args) | |
| end | |
| def authenticity_token | |
| %Q{<input type="hidden" name="authenticity_token" value="#{session[:csrf]}"/>} | |
| end | |
| end | |
| configure :development do | |
| require "sinatra/reloader" | |
| register Sinatra::Reloader | |
| also_reload 'app/**/*.rb' | |
| also_reload 'lib/**/*.rb' | |
| also_reload 'conf/**/*.rb' | |
| set :raise_errors, true | |
| end | |
| [:error, :info, :success].each do |key| | |
| class_eval " | |
| def flash_#{key}(key, now=true) | |
| message(key, :#{key}, now) | |
| end | |
| " | |
| end | |
| def message(key, type=:notice, now=true) | |
| hash = now ? flash.now : flash | |
| hash[type] = I18n.t(key) | |
| end | |
| def debug_something_with_pry | |
| Kernel.binding.pry | |
| end | |
| error do | |
| slim :'errors/500' | |
| end | |
| not_found do | |
| slim :'errors/404' | |
| end | |
| error ActiveRecord::RecordNotFound do | |
| slim :'errors/404' | |
| end | |
| before do | |
| I18n.locale = params[:locale] || I18n.default_locale | |
| end | |
| end | |
| # Require attr_accessible... | |
| ActiveRecord::Base.send(:attr_accessible, nil) | |
| # Move to config/init/db.rb if you like | |
| OpenStruct.new(YAML::load(File.open('config/database.yml'))[App.environment.to_s].symbolize_keys).tap do |config| | |
| ActiveRecord::Base.establish_connection( | |
| host: config.host, | |
| adapter: config.adapter, | |
| database: config.database, | |
| username: config.username, | |
| password: config.password | |
| ) | |
| end | |
| %w(models controllers concerns).each do |name| | |
| Dir[File.join('app', name, '**/*.rb')].each do |file| | |
| require_relative file | |
| end | |
| end | |
| # Move to config/init/i18n.rb if you like | |
| Dir[File.join(App.root, 'config', 'locales', '*.yml')].each do |file| | |
| I18n.backend.load_translations(file) | |
| end | |
| I18n.default_locale = :en | |
| # Move to app/controllers/root_controller.rb | |
| class RootController < App | |
| get '/' do | |
| flash_alert "Thanks for nothing" | |
| slim :'index' | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require './app' | |
| map "/" do | |
| run RootController | |
| end | |
| map "/assets" do | |
| run App.sprockets | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| development: | |
| adapter: mysql2 | |
| database: xxx_development | |
| username: root | |
| password: | |
| host: localhost |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| source :rubygems | |
| gem 'thin' | |
| gem 'slim' | |
| gem 'sass' | |
| gem 'sinatra' | |
| gem 'sprockets' | |
| gem 'sinatra-partial' | |
| gem 'coffee-script' | |
| gem 'therubyracer' | |
| group :development do | |
| gem 'sinatra-reloader' | |
| gem 'sinatra-activerecord' | |
| gem 'pry' | |
| end | |
| gem 'rack-flash3' | |
| gem 'awesome_print' | |
| gem 'mysql2' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # run | |
| mkdir -p app/controllers app/models app/concerns app/views/layouts app/views/css app/views/js lib tmp log public/img public/js public/css config/init db/migrate lib/tasks | |
| bundle | |
| bundle exec thin start -p 3000 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'bundler/setup' | |
| require 'sinatra/activerecord/rake' | |
| require 'pry' | |
| require './app' | |
| Dir[File.join('lib', 'tasks', '**', '*.rake')].each do |file| | |
| import file | |
| end | |
| task :console do | |
| binding.pry | |
| end | |
| # Asset pipeline (Sprockets) | |
| namespace :assets do | |
| task :compile do | |
| App.sprockets['application.js'].write_to('public/assets/application.js') | |
| App.sprockets['application.css'].write_to('public/assets/application.css') | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment