Movatterモバイル変換


[0]ホーム

URL:


Upgrade to Pro — share decks privately, control downloads, hide ads and more …
Speaker DeckSpeaker Deck
Speaker Deck

[RailsConf 2023 Opening Keynote] The Magic of R...

Avatar for Eileen M. Uchitelle Eileen M. Uchitelle
April 24, 2023

[RailsConf 2023 Opening Keynote] The Magic of Rails

Today we're going to explore the magic of Rails. We'll look at the philosophy behind the framework as well as the overall structure of the components. We'll explore some of the common patterns that Rails uses to build agnostic and beautiful interfaces, and the techniques it implements to hide complexity so you can focus building your application. By the end of this talk you'll feel more confident navigating the Rails codebase and better understand the patterns it uses to create the framework we all know and love. But Rails is so much more than its design and architecture. We'll dive into my motivations for working on the framework and why the community is so important to the long term success of Rails.

Avatar for Eileen M. Uchitelle

Eileen M. Uchitelle

April 24, 2023
Tweet

More Decks by Eileen M. Uchitelle

See All by Eileen M. Uchitelle

Other Decks in Programming

See All in Programming

Featured

See All Featured

Transcript

  1. The Magic of Rails exploring the principles & techniques behind

    the framework
  2. None
  3. Hello RailsConf! I'm Eileen M. Uchitelle @eileencodes @[email protected]

  4. None
  5. None
  6. The Magic of Rails exploring the principles & techniques behind

    the framework
  7. What is Ruby on Rails?

  8. Rails is modular, but 
 not fractured

  9. Rails is designed to have agnostic interfaces

  10. Rails is extracted from applications

  11. Rails is made of 
 simple and aesthetic APIs

  12. Rails is a framework that takes on complexity to empower

    you
  13. How Rails components are structured

  14. RUBY ON RAILS Active Record Active Support Active Model Active

    Job Active Storage Action Mailer Action Pack Action View Action Cable Action Text Action Mailbox Railties
  15. Naming Convention Active vs Action

  16. Active Record Active Support Active Model Active Job Active Storage

    BACKEND NAMING CONVENTION
  17. USER FACING Active Record Active Support Active Model Active Job

    Active Storage BACKEND NAMING CONVENTION Action Mailer Action Pack Action View Action Cable Action Text Action Mailbox USER FACING
  18. Active Record Active Support Active Model Active Job Active Storage

    Action Mailer Action Pack Action View Action Cable Action Text Action Mailbox BACKEND USER FACING Railties GLUE NAMING CONVENTION
  19. Architeture & Patterns of Rails components

  20. Architecture & Patterns the role of Railties

  21. 👩💻 Application

  22. 👩💻 Application Register hooks

  23. 👩💻 Application Register hooks Load components

  24. 👩💻 Application Register hooks Load components Run hooks

  25. # railtie.rb initializer "initializer.name" do # do something at initialization

    end
  26. # railties/lib/rails/application.rb def initializer(name, opts = {}, &block) self.class.initializer(name, opts,

    &block) end
  27. # railtie.rb initializer "initializer.name" do |app| app.do_something app.config.do_something end

  28. # railtie.rb initializer "initializer.name" do ActiveSupport.on_load(:active_record) do # do something

    at initialization end end
  29. # activerecord/lib/active_record/railtie.rb initializer "active_record.initialize_database" do ActiveSupport.on_load(:active_record) do self.configurations = Rails.application.config.database_configuration

    establish_connection end end
  30. # activerecord/lib/active_record/railtie.rb initializer "active_record.initialize_database" do ActiveSupport.on_load(:active_record) do self.configurations = Rails.application.config.database_configuration

    establish_connection end end
  31. # activerecord/lib/active_record/railtie.rb initializer "active_record.initialize_database" do ActiveSupport.on_load(:active_record) do self.configurations = Rails.application.config.database_configuration

    establish_connection end end
  32. # activejob/lib/active_job/railtie.rb initializer "active_job.logger" do ActiveSupport.on_load(:active_job) { self.logger = ::Rails.logger

    } end
  33. # activejob/lib/active_job/railtie.rb initializer "active_model.deprecator", before: :load_environment_config do |app| app.deprecators[:active_model] =

    ActiveModel.deprecator end
  34. # activejob/lib/active_job/railtie.rb initializer "active_model.deprecator", before: :load_environment_config do |app| app.deprecators[:active_model] =

    ActiveModel.deprecator end
  35. • Railties are the core of the framework

  36. • Railties are the core of the framework • Railties

    control load order and when hooks should be run
  37. • Railties are the core of the framework • Railties

    control load order and when hooks should be run • Enables components to work together without adding dependencies
  38. Architecture & Patterns Agnostic interfaces

  39. if connection.is_a?(PostgresqlAdapter) # ... elsif connection.is_a?(Mysql2Adapter) # ... elsif connection.is_?(Sqlite3Adapter)

    # ... else # ... end
  40. if connection.is_a?(PostgresqlAdapter) # ... elsif connection.is_a?(Mysql2Adapter) # ... elsif connection.is_?(Sqlite3Adapter)

    # ... else # ... end ❌
  41. module ActiveRecord module ConnectionAdapters class AbstractAdapter # define interface end

    end end
  42. module ActiveRecord module ConnectionAdapters class AbstractAdapter # define interface end

    end end module ActiveRecord module ConnectionAdapters class PostgresqlAdapter < AbstractAdapter # inherit or redefine interface end end end
  43. connection.supports_foreign_keys? => true

  44. class AbstractAdapter def supports_foreign_keys? false end end

  45. class AbstractAdapter def supports_foreign_keys? false end end class PostgresqlAdapter <

    AbstractAdapter def supports_foreign_keys? true end end
  46. # activestorage/lib/active_storage/service.rb module ActiveStorage class Service def delete(key) raise NotImplementedError

    end end end
  47. # activestorage/lib/active_storage/service/gcs_service.rb class ActiveStorage class Service::GCSService < Service def delete(key)

    instrument :delete, key: key do file_for(key).delete rescue Google::Cloud::NotFoundError # Ignore files already deleted end end end end
  48. @service.delete(key)

  49. • Consistent interface for all supported libraries

  50. • Consistent interface for all supported libraries • Simpli fi

    es Rails code to avoid using `is_a?`
  51. • Consistent interface for all supported libraries • Simpli fi

    es Rails code to avoid using `is_a?` • Makes it easy for apps to swap out adapters / services
  52. • Consistent interface for all supported libraries • Simpli fi

    es Rails code to avoid using `is_a?` • Makes it easy for apps to swap out adapters / services • Lowers the maintenance burden
  53. Migrating Shopify's Core Rails Monolith to Trilogy Adrianna Chang Monday,

    April 24 @ 3pm
  54. Architecture & Patterns Metaprogramming

  55. class Post < ApplicationRecord has_many :comments end class Comment <

    ApplicationRecord belongs_to :post end
  56. post = Post.first post.comments => [#<Comment:0x000000010e353838...>, #<Comment:0x000000010e3530e0>]

  57. post = Post.first post.method(:comments).source_location

  58. post = Post.first post.method(:comments).source_location => ["rails/activerecord/lib/ active_record/associations/builder/ association.rb", 103]

  59. # activerecord/lib/active_record/associations/builder/ association.rb class ActiveRecord::Associations::Builder class Association def self.define_readers(mixin, name)

    mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} association(:#{name}).reader end CODE end end end
  60. # activerecord/lib/active_record/associations/builder/ association.rb class ActiveRecord::Associations::Builder class Association def self.define_readers(mixin, name)

    mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} association(:#{name}).reader end CODE end end end
  61. # activerecord/lib/active_record/associations/builder/ association.rb class ActiveRecord::Associations::Builder class Association def self.define_readers(mixin, name)

    mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} association(:#{name}).reader end CODE end end end
  62. Post::GeneratedAssociationMethods

  63. # activerecord/lib/active_record/associations/builder/ association.rb class ActiveRecord::Associations::Builder class Association def self.define_readers(mixin, name)

    mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name} association(:#{name}).reader end CODE end end end
  64. # activerecord/lib/active_record/associations/builder/ association.rb class ActiveRecord::Associations::Builder class Association def self.define_writers(mixin, name)

    mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}=(value) association(:#{name}).writer(value) end CODE end end end
  65. # activerecord/lib/active_record/associations/builder/ association.rb class ActiveRecord::Associations::Builder class Association def self.define_writers(mixin, name)

    mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1 def #{name}=(value) association(:#{name}).writer(value) end CODE end end end
  66. • Powerful tool that enables us to build beautiful, simple

    APIs
  67. • Powerful tool that enables us to build beautiful, simple

    APIs • Hides complexity from your application
  68. • Powerful tool that enables us to build beautiful, simple

    APIs • Hides complexity from your application • Where "Rails Magic" comes from
  69. Maintaining Rails Why I work on it

  70. 2010 Introduced to Rails

  71. 2010 Introduced to Rails 2011 Big Nerd Ranch

  72. 2010 Introduced to Rails 2011 Big Nerd Ranch 2014 1st

    conference 1st contribution
  73. 2010 Introduced to Rails 2011 Big Nerd Ranch 2014 1st

    conference 1st contribution 2015 First RailsConf
  74. 2010 Introduced to Rails 2011 Big Nerd Ranch 2014 1st

    conference 1st contribution 2015 First RailsConf 2017 Join Rails Core
  75. 2010 Introduced to Rails 2011 Big Nerd Ranch 2014 1st

    conference 1st contribution 2015 First RailsConf 2017 Join Rails Core 2023 This RailsConf
  76. I work on Rails to advance the framework

  77. I work on Rails to ensure applications can stay on

    Rails
  78. I work on Rails to build a stronger community

  79. I work on Rails to have an impact on the

    future
  80. Rails is so much more than just a framework

  81. Rails is inspiring

  82. Rails is empowering

  83. Rails is imperfect

  84. Rails is the applications we build

  85. Rails is the team behind it

  86. Rails is the community

  87. Rails is magic

  88. Thank You!


[8]ページ先頭

©2009-2025 Movatter.jp