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

無理しないRails

いも
March 08, 2017

 無理しないRails

社内Techカンファレンスで発表した資料です。

いも

March 08, 2017
Tweet

More Decks by いも

Other Decks in Technology

Transcript

  1. ౰࣌ͷߏ੒Λ؆ུԽͨ͠΋ͷ ཧ૝ MPMJQPQKQ VTFSMPMJQPQKQ ސ٬؅ཧ -PMJQPQ"1* 4FSWFS"1* αʔόαΠυ όοΫΤϯυ ϑϩϯτ

    Ϣʔβʹఏڙ͢ΔྖҬΛѻ͏ ɾ8&#αʔό ɾ.BJMαʔό ɾ.Z42-αʔόFUD Ϣʔβͷސ٬৘ใͳͲΛѻ͏ ɾΞΧ΢ϯτ৘ใ ɾݸਓ৘ใ ɾܖ໿৘ใFUD
  2. ౰࣌ͷߏ੒Λ؆ུԽͨ͠΋ͷ ݱ࣮ MPMJQPQKQ VTFSMPMJQPQKQ ސ٬؅ཧ -PMJQPQ"1* 4FSWFS"1* αʔόαΠυ όοΫΤϯυ ϑϩϯτ

    Ϣʔβʹఏڙ͢ΔྖҬΛѻ͏ ɾ8&#αʔό ɾ.BJMαʔό ɾ.Z42-αʔόFUD Ϣʔβͷސ٬৘ใͳͲΛѻ͏ ɾΞΧ΢ϯτ৘ใ ɾݸਓ৘ใ ɾܖ໿৘ใFUD ςετ΄΅ແ͠ ςετແ͠ ςετແ͠ ςετແ͠ ϩδοΫ ϩδοΫ ϩδοΫ
  3. ͜͏͢Δͧʂ MPMJQPQKQ VTFSMPMJQPQKQ ސ٬؅ཧ "1* 4FSWFS"1* αʔόαΠυ όοΫΤϯυ ϑϩϯτ Ϣʔβʹఏڙ͢ΔྖҬΛѻ͏

    ɾ8&#αʔό ɾ.BJMαʔό ɾ.Z42-αʔόFUD Ϣʔβͷސ٬৘ใͳͲΛѻ͏ ɾΞΧ΢ϯτ৘ใ ɾݸਓ৘ใ ɾܖ໿৘ใFUD ςετ΄΅ແ͠ ςετແ͠ ςετແ͠ ςετແ͠ MPMMJQPQSC
  4. ͕

  5. ಠࣗϧʔϧ͸$PODFSOʹ·ͱΊͯΈΔ module LegacyDateFormattable extend ActiveSupport::Concern included do class << self

    attr_reader :legacy_dates def legacy_dates=(dates) @legacy_dates = dates define_methods_for_generize_date end def define_methods_for_generize_date # ΧϥϜ໊Λ্ॻ͖ͯ͠DateΦϒδΣΫτฦ͢ϝιουΛੜ੒ end end end end class Contract < ActiveRecord::Base include LegacyDateFormattable self.legacy_dates = %i( start_date end_date ) end w ྫ͑͹ಠࣗͷ೔෇ΧϥϜ͸ɺ4USJOH%BUFͰ ૬ޓʹม׵͢Δ$PDFSOϞδϡʔϧΛ࣮૷͍ͯ͠ Δ w ࠾൪ॲཧͳͲ΋BQQNPEFMTDPODFSOTԼʹϞ δϡʔϧΛ௥Ճ͍ͯ͠Δ w ϞδϡʔϧΛ௥Ճ͓͚ͯ͠͹ɺಠࣗϧʔϧΛ͋· Γؾʹͤͣී௨ͷϞσϧͱͯ͠;Δ·͑Δ w ΍΍ϝλϓϩʹͳΓ͕ͪ
  6. Ϟσϧͷఆٛ class Hogedb::Base < ActiveRecord::Base self.abstract_class = true establish_connection Lolipop::Database.load_configuration(:hoge_db)[Rails.env.to_s]

    end w Ҿ਺ͰΑ͠ͳʹ઀ଓ৘ใͷ:".-Λϩʔυͯ͠઀ଓ͢Δ w ֤ςʔϒϧͷϞσϧ͸#BTFΛܧঝ࣮ͯ͠૷͢Δ
  7. ઀ଓ৘ใͱεΩʔϚΛ഑ஔ config ├── application.rb ├── environment.rb ├── environments/ ├── database.yml

    ├── hogedb_database.yml ├── fugadb_database.yml ├── foodb_database.yml ├── bardb_database.yml ├── routes.rb └── secrets.yml db ├── others │ ├── hogedb │ │ └── structure.sql │ ├── fugadb │ │ └── structure.sql │ ├── foodb │ │ └── structure.sql │ └── bardb │ ├── seeds.rb │ └── structure.sql ├── seeds.rb └── structure.sql
  8. PUIFS@ECͷηοτΞοϓλεΫΛ࡞Δ other_databases = %w(hogedb fugadb) other_databases.each do |db| current_namespace =

    namespace db.to_sym do task :load_config do end desc "Creates the #{db}" task :create => [:load_config] do end desc "Drops the #{db}" task :drop => [:load_config] do end namespace :structure do task :load => [:create, :environment, :load_config] do end end desc "Creates the #{db} and load schema" task :setup => ["#{db}:structure:load"] end end جຊ͸3BJMTͷ%BUBCBTFSBLFΛࢀߟʹ͢Δ
  9. ෳ਺%#ΛҰׅηοτΞοϓ namespace :multi_db do @databases = %w(hogedb fugadb foodb) desc

    'Sets up all databases' task :setup do @databases.each { |database| Rake::Task["#{database}:setup"].invoke } end desc 'Creates all databases' task :create do @databases.each { |database| Rake::Task["#{database}:create"].invoke } end desc 'Drops all databases' task :drop do @databases.each { |database| Rake::Task["#{database}:drop"].invoke } end end NVMUJ@ECTFUVQͰશ%#͕࡞ΒΕγʔυ஋͕ྲྀ͠ࠐ·ΕΔ
  10. ׬શ3BJMTԽ αʔόαΠυ ϑϩϯτ MPMJQPQKQ MPMJQPQSC VTFSMPMJQPQKQ MPMJQPQSC ސ٬؅ཧ MPMJQPQSC 4FSWFS"1*

    Ϣʔβʹఏڙ͢ΔྖҬΛѻ͏ ɾ8&#αʔό ɾ.BJMαʔό ɾ.Z42-αʔόFUD