Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ambiguous Sinatra - Vadim Evseev

64 views

Published on

Ruby Meditation #20
February 17, 2018
Kyiv

Published in: Software
  • Be the first to comment

  • Be the first to like this

Ambiguous Sinatra - Vadim Evseev

  1. 1. Ambiguous Sinatra
  2. 2. Ambiguous Sinatra The story about difficulties, surprises and workarounds using Sinatra for complex projects. Story about walking through the mines field from the person who started learning Ruby from RoR framework.
  3. 3. Sinatra
  4. 4. Web project 1. Models, ORM, migrations 2. Routes 3. Assets pipeline 4. Forms, helpers 5. Console 6. Code load 7. Security, CSRF
  5. 5. Cost of support Other
  6. 6. Models SinatraRails ActiveRecord models
  7. 7. Jeremy Evans
  8. 8. Models Sinatra Sequel Rails ActiveRecord models ActiveRecord /
  9. 9. Models Sinatra Sequel models Rails ActiveRecord models dependent options dependent options Sequel::Model.plugin :association_dependencies plugin :association_dependencies, some_model: :destroy counter cache
  10. 10. Models Sinatra Sequel models Rails ActiveRecord models counter cache dependent optionsdependent options counter cache
  11. 11. Models Sinatra Sequel models Rails ActiveRecord models counter cache require 'sequel_postgresql_triggers' dependent optionsdependent options counter cache
  12. 12. too much bicycles
  13. 13. ORM Rails Sinatra ActiveRecord, Arel
  14. 14. Jeremy Evans
  15. 15. ORM Rails Sinatra ActiveRecord, Arel Sequel
  16. 16. ORM Rails Sinatra ActiveRecord, Arel Sequel gem 'sequel_pg', :require=>'sequel' overwrites the inner loop of the Sequel postgres adapter row fetching code with a C version
  17. 17. ORM Rails Sinatra ActiveRecord, Arel Sequel gem 'sequel_pg', :require=>'sequel' plugin pg_json plugin tree, rcte_tree
  18. 18. Sequel
  19. 19. Migrations Sinatra rails db:migrate Rails ActiveRecord migrations
  20. 20. Migrations Rails Sinatra ActiveRecord migrations Sequel migrations sequel -m relative/path/to/migrations DBrails db:migrate namespace :db do desc 'Run migrations' task :migrate, [:ver] do |_t, args| Sequel.extension :migration require ‘my_app_lib’     if args[:ver] puts "Migrating to version #{args[:ver]}" Sequel::Migrator.run(DB, 'db/ migrations', target: args[:ver].to_i) else puts 'Migrating to latest' Sequel::Migrator.run(DB, 'db/ migrations') end end end
  21. 21. writing migrations tasks
  22. 22. Routes Rails Sinatra by design gem 'sinatra_more' require 'sinatra_more' class Application < Sinatra::Base register SinatraMore::MarkupPlugin register SinatraMore::RenderPlugin register SinatraMore::WardenPlugin register SinatraMore::MailerPlugin register SinatraMore::RoutingPlugin end
  23. 23. Routes Rails Sinatra by design gem 'sinatra_more' • code generators for creating new sinatra applications (sinatra_gen) • generic view and tag helpers (tag, content_tag, input_tag, …) • asset tag helpers (link_to, image_tag, javascript_include_tag, …) • full form helpers (form_tag, form_for, field_set_tag, text_field, …) • full url named route support • formatting extensions (relative_time_ago, js_escape_html, sanitize_html) • simple 'mailer' support for sinatra (powered by pony) • plug and play setup for the excellent Warden authentication system
  24. 24. development has stopped sinatra_more
  25. 25. Assets pipeline Rails Sinatra by design gem 'sinatra-asset-pipeline' require 'sinatra/asset_pipeline' class App < Sinatra::Base register Sinatra::AssetPipeline … some code … end use sprockets-helpers
  26. 26. Forms, helpers Rails Sinatra by design, Action View
  27. 27. Jeremy Evans
  28. 28. Forms, helpers Rails Sinatra by design, Action View gem 'forme'
  29. 29. Forms, helpers Rails Sinatra by design, Action View gem ‘forme’ + SLIM
  30. 30. Forms, helpers Rails Sinatra by design, Action View gem ‘forme’ + SLIM
  31. 31. Code load Rails Sinatra Autoloading and Reloading Constants Sinatra::Reloader require "sinatra/reloader"config.cache_classes = false require "sinatra/base" require "sinatra/reloader" class MyApp < Sinatra::Base configure :development do register Sinatra::Reloader also_reload '/path/to/some/file' dont_reload '/path/to/other/file' after_reload do puts 'reloaded' end end ... some code ... end
  32. 32. Code load Rails Sinatra Autoloading and Reloading Constants gem ‘rerun'config.cache_classes = false
  33. 33. Code load Rails Sinatra Autoloading and Reloading Constants gem ‘rerun'config.cache_classes = false rerun ruby ./app.rb
  34. 34. Code load Rails Sinatra Autoloading and Reloading Constants config.cache_classes = false require "sinatra/reloader" gem ‘rerun'
  35. 35. Sinatra Code load
  36. 36. Console Rails Sinatra require 'bundler/setup' Bundler.require(:default) require File.dirname(__FILE__) + "/lib/app_base.rb" require File.dirname(__FILE__) + "/lib/app_other.rb" map "/" do run AppBase end map "/another" do run AppOther end rails c irb -r ./app.rb config.ru
  37. 37. Console Rails Sinatra rails c irb -r ./app.rb
  38. 38. Modular application Rails Sinatra by design config.ru
  39. 39. Modular application Rails Sinatra module MyApp   class App < Sinatra::Base ...some code... use MyApp::Controllers::SomeController ...some code... end end include MyApp by design config.ru app.rb
  40. 40. Specs Rails Sinatra rspec + turnip + factory_bot … sequel + factory_bot
  41. 41. Specs Rails Sinatra rspec + turnip + factory_bot … sequel + factory_bot NoMethodError: undefined method `save!' for #<Model:...>
  42. 42. Specs Rails Sinatra rspec + turnip + factory_bot … sequel + factory_bot NoMethodError: undefined method `save!' for #<Model:...> a) call FactoryGirl.build(:symbol) to build a Model instance and after call that create() b) add into FactoryGirl model definition: to_create { |i| i.save } or
  43. 43. Specs Rails Sinatra rspec + turnip + factory_bot … sequel + factory_bot
  44. 44. Security, CSRF Rails Sinatra CSRF tokens, security updates gem 'rack_csrf' require "rack/csrf" configure do use Rack::Session::Cookie, secret: "some secret" use Rack::Csrf, raise: true end <form method="post" action="/tweet"> <%= Rack::Csrf.csrf_tag(env) %> <input type="text" name="message"/> <input type="submit" value="Submit a tweet!"/> </form>
  45. 45. Installing CSRF
  46. 46. Cost of support Rails Sinatra
  47. 47. WE ARE NEED YOUR MONEY
  48. 48. Rails Sinatra Cost of support
  49. 49. Jeremy Evans
  50. 50. Rails Sinatra Cost of support

×