Slide 1

Slide 1 text

OGUCHI Haruka @hogucc Oct 21 2022 Kaigi on Rails 2022

Slide 2

Slide 2 text

Haruka Oguchi @hogucc Fintech × SaaS enpay Rails Go

Slide 3

Slide 3 text

🧙

Slide 4

Slide 4 text

gem

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

🔮 🔮 🔮 🔮

Slide 7

Slide 7 text

Ruby 2 send, public_send eval, instance_eval, module_eval, class_eval de fi ne_method, de fi ne_singleton_method, method_missing

Slide 8

Slide 8 text

Ruby Ruby include, exclude, prepend Ruby

Slide 9

Slide 9 text

🔍

Slide 10

Slide 10 text

devise current_xxx devise Rails gem https://github.com/heartcombo/devise current_( ) User ⾒ current_user User Account ⾒ current_account

Slide 11

Slide 11 text

current_user

Slide 12

Slide 12 text

current_user user current_

Slide 13

Slide 13 text

README gem README ⾒ devise current_xxx devise_for current_user routes devise_for

Slide 14

Slide 14 text

include, exclude, prepend de fi ne_method

Slide 15

Slide 15 text

current_user 🔍 module ActionDispatch::Routing class RouteSet prepend Devise::RouteSet end class Mapper def devise_for(*resources) … options = resources.extract_options! … resources.map!(&:to_sym) resources.each do |resource| mapping = Devise.add_mapping(resource, options) … /lib/devise/rails/routes.rb devise_for :users, skip: :all

Slide 16

Slide 16 text

module ActionDispatch::Routing class RouteSet prepend Devise::RouteSet end class Mapper def devise_for(*resources) … resources.each do |resource| mapping = Devise.add_mapping(resource, options) … /lib/devise/rails/routes.rb devise_for :users, skip: :all resources current_user 🔍

Slide 17

Slide 17 text

module ActionDispatch::Routing def self.add_mapping(resource, options) mapping = Devise::Mapping.new(resource, options) … @@helpers.each { |h| h.de fi ne_helpers(mapping) } mapping end … current_user 🔍

Slide 18

Slide 18 text

def self.de fi ne_helpers(mapping) mapping = mapping.name class_eval <<-METHODS, __FILE__, __LINE__ + 1 … def current_#{mapping} @current_#{mapping} ||= warden.authenticate(scope: :#{mapping}) end … METHODS … end lib/devise/controllers/helpers.rb current_user current_user 🔍

Slide 19

Slide 19 text

gem

Slide 20

Slide 20 text

Rails

Slide 21

Slide 21 text

Rails app/ https://zenn.dev/takahashim/articles/ ac 725 fb 16 ec 7 a 11 8 09 c 5

Slide 22

Slide 22 text

GitHub send, public_send eval, instance_eval, module_eval, class_eval de fi ne_method de fi ne_singleton_method method_missing

Slide 23

Slide 23 text

⾒ clone

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

con fi g settings.yml

Slide 27

Slide 27 text

lib/site_settings/deprecated_settings.rb module SiteSettings::DeprecatedSettings … def setup_deprecated_methods SETTINGS.each do |old_setting, new_setting, override, version| … de fi ne_singleton_method old_setting do |warn: true| If warn Discourse.deprecate( `SiteSetting.#{old_setting}` has been deprecated. Please use `SiteSetting.#{new_setting}` instead. , drop_from: version ) end self.public_send(override ? new_setting : "_#{old_setting}") end … de fi ne_singleton_method

Slide 28

Slide 28 text

module SiteSettings::DeprecatedSettings SETTINGS = [ # [, , , ] [ search_tokenize_chinese_japanese_korean', 'search_tokenize_chinese', true, 2 . 9 '], ['default_categories_regular', 'default_categories_normal', true, 3 . 0 '], ] def setup_deprecated_methods SETTINGS.each do |old_setting, new_setting, override, version| de fi ne_singleton_method old_setting do |warn: true| … end end … old_setting new_setting lib/site_settings/deprecated_settings.rb

Slide 29

Slide 29 text

module SiteSettings::DeprecatedSettings fi ne_singleton_method old_setting do |warn: true| If warn Discourse.deprecate( `SiteSetting.#{old_setting}` has been deprecated. Please use `SiteSetting.#{new_setting}` instead. , drop_from: version ) end self.public_send(override ? new_setting : "_#{old_setting}") end … Discourse.deprecate Rails.logger.warn lib/site_settings/deprecated_settings.rb

Slide 30

Slide 30 text

module SiteSettings::DeprecatedSettings fi ne_singleton_method old_setting do |warn: true| If warn # end self.public_send(override ? new_setting : "_#{old_setting}") end … public_send String Symbol lib/site_settings/deprecated_settings.rb

Slide 31

Slide 31 text

Redmine HTML macro_xxx xxx HTML macro_issue_link ⾒ public_send xxxPolicy

Slide 32

Slide 32 text

⾒ de fi ne_singleton_method public_send xxx_counter_increment! xxx

Slide 33

Slide 33 text

Ruby Rails ⾒ Rails

Slide 34

Slide 34 text

No content