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

viewing ruby blossom rdrc2017

viewing ruby blossom rdrc2017

Anton Davydov

June 23, 2017
Tweet

More Decks by Anton Davydov

Other Decks in Programming

Transcript

  1. hanami (花⾒見見) Hanami (花⾒見見, lit. "flower viewing") is the Japanese

    traditional custom of enjoying the transient beauty of flowers, flowers ("hana") in this case almost always referring to those of the cherry ("sakura") or, less frequently, plum ("ume") trees.
  2. apps/ ├── admin │ ├── application.rb │ ├── assets │

    │ └── ... │ ├── config │ │ └── ... │ ├── controllers │ │ └── ... │ ├── templates │ │ └── ... │ └── views │ └── ... └── web ├── ...
  3. apps/ ├── admin │ ├── application.rb │ ├── assets │

    │ └── ... │ ├── config │ │ └── ... │ ├── controllers │ │ └── ... │ ├── templates │ │ └── ... │ └── views │ └── ... └── web ├── ...
  4. apps/ ├── admin │ ├── application.rb │ ├── assets │

    │ └── ... │ ├── config │ │ └── ... │ ├── controllers │ │ └── ... │ ├── templates │ │ └── ... │ └── views │ └── ... └── web ├── ...
  5. lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb

    │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories │ └── user_repository.rb └── project_name.rb
  6. lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb

    │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories │ └── user_repository.rb └── project_name.rb
  7. lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb

    │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories │ └── user_repository.rb └── project_name.rb
  8. lib/ ├── project_name │ ├── interactors │ │ └── create_user.rb

    │ ├── entities │ │ └── user.rb │ ├── mailers │ │ └── templates │ └── repositories │ └── user_repository.rb └── project_name.rb
  9. hanami - Base repository, CLI router - Rack compatible HTTP

    router for Ruby controller - Full featured and fast actions for Rack utils - Ruby core extensions and class utilities model - Persistence with entities and repositories
  10. validations - Validations mixin for Ruby objects helpers - View

    helpers for Ruby applications view - Presentation with a separation assets - Assets management for Ruby mailer - Mail for Ruby applications
  11. # hanami-router class HelloApp def call(env) [200, { **env },

    ['Hello!']] end end router = Hanami::Router.new router.get '/', to: 'hello_app'
  12. class InstitutionsController < ApplicationController load_and_authorize_resource :only => [:destroy,:edit,:new,:create,:update] before_filter :authenticate_user!,

    :except => [:student_registration, :show, :validate_registration_pin, :result, :admission, :buy_registration_pin,:paygate_callback_failure, :p aygate_cancel, :paygate_pending, :paygate_callback_success, :pin_transaction_info_print] before_filter :find_institution, :except => [:show,:index, :new, :create, :semesters_for_institute_type, :start_end_date_for_assessment_period, :courses_for_batch, :paygate_ca llback_failure, :paygate_cancel, :paygate_pending, :paygate_callback_success, :pin_transaction_info_print] before_filter :add_bread_crumb,:except => [:show] def paygate_callback_success @pay_gate_config = YAML::load(File.open("#{Rails.root}/config/pay_gate_config.yml"))[Rails.env] @payment = TransactionRecord.find_by_order_number(params[:OrderID]) uri = URI("https://fidelitypaygate.fidelitybankplc.com/cipg/MerchantServices/UpayTransactionStatus.ashx") parameters = {:MERCHANT_ID => "#{@pay_gate_config['merchant_id']}", :ORDER_ID => "#{@payment.order_number}"} uri.query = URI.encode_www_form(parameters) result =open(uri).read result_hash = Hash.from_xml(result) record_payment_details(result_hash) if result_hash["CIPG"]["StatusCode"] == PaymentRecord::PAYMENT_SUCCESS_CODE if @payment.transactionable_type.eql?("PaymentRecord") redirect_to institution_fees_path(@payment.transactionable_type.fee.institution), :notice => "Payment transaction has been #{result_hash['CIPG']['Status']}" elsif @payment.transactionable_type.eql?("PinBuyerInfo") unless @payment.transactionable.pin_id.present? @registration = @payment.transactionable.registration @valid_registration_pin_groups = @registration.valid_registration_pin_groups @online_valid_registration_pin_groups = @valid_registration_pin_groups.where(:pin_available_type => 'Online') @offline_valid_registration_pin_groups = @valid_registration_pin_groups.where(:pin_available_type => 'Offline') @available_pin = nil @online_valid_registration_pin_groups.each do |vpg| if vpg.available_pins.present? @available_pin = vpg.available_pins.first break else next end end if !@available_pin.present? @offline_valid_registration_pin_groups.each do |vpg| if vpg.available_pins.present? @available_pin = vpg.available_pins.first break else next end end
  13. class InstitutionsController < ApplicationController load_and_authorize_resource :only => [:destroy,:edit,:new,:create,:update] before_filter :authenticate_user!,

    :except => [:student_registration, :show, :validate_registration_pin, :result, :admission, :buy_registration_pin,:paygate_callback_failure, :p aygate_cancel, :paygate_pending, :paygate_callback_success, :pin_transaction_info_print] before_filter :find_institution, :except => [:show,:index, :new, :create, :semesters_for_institute_type, :start_end_date_for_assessment_period, :courses_for_batch, :paygate_ca llback_failure, :paygate_cancel, :paygate_pending, :paygate_callback_success, :pin_transaction_info_print] before_filter :add_bread_crumb,:except => [:show] def paygate_callback_success @pay_gate_config = YAML::load(File.open("#{Rails.root}/config/pay_gate_config.yml"))[Rails.env] @payment = TransactionRecord.find_by_order_number(params[:OrderID]) uri = URI("https://fidelitypaygate.fidelitybankplc.com/cipg/MerchantServices/UpayTransactionStatus.ashx") parameters = {:MERCHANT_ID => "#{@pay_gate_config['merchant_id']}", :ORDER_ID => "#{@payment.order_number}"} uri.query = URI.encode_www_form(parameters) result =open(uri).read result_hash = Hash.from_xml(result) record_payment_details(result_hash) if result_hash["CIPG"]["StatusCode"] == PaymentRecord::PAYMENT_SUCCESS_CODE if @payment.transactionable_type.eql?("PaymentRecord") redirect_to institution_fees_path(@payment.transactionable_type.fee.institution), :notice => "Payment transaction has been #{result_hash['CIPG']['Status']}" elsif @payment.transactionable_type.eql?("PinBuyerInfo") unless @payment.transactionable.pin_id.present? @registration = @payment.transactionable.registration @valid_registration_pin_groups = @registration.valid_registration_pin_groups @online_valid_registration_pin_groups = @valid_registration_pin_groups.where(:pin_available_type => 'Online') @offline_valid_registration_pin_groups = @valid_registration_pin_groups.where(:pin_available_type => 'Offline') @available_pin = nil @online_valid_registration_pin_groups.each do |vpg| if vpg.available_pins.present? @available_pin = vpg.available_pins.first break else next end end if !@available_pin.present? @offline_valid_registration_pin_groups.each do |vpg| if vpg.available_pins.present? @available_pin = vpg.available_pins.first break else next end end
  14. class UsersController < AC def new end def send_sms end

    def user_params end end Controllers: Rails
  15. class Cats::CruelsController < AC def index end def show end

    def user_params end end Controllers: DHH style
  16. class User < ActiveRecord::Base
 include Gravtastic before_destroy :yank_gems has_many :rubygems,

    through: :ownerships validates :name, presence: true # ... end Model: Rails
  17. class User < ActiveRecord::Base
 include Gravtastic before_destroy :yank_gems has_many :rubygems,

    through: :ownerships validates :name, presence: true # ... end Model: Rails
  18. class User < ActiveRecord::Base
 include Gravtastic before_destroy :yank_gems has_many :rubygems,

    through: :ownerships validates :name, presence: true # ... end Model: Rails
  19. class User < ActiveRecord::Base
 include Gravtastic before_destroy :yank_gems has_many :rubygems,

    through: :ownerships validates :name, presence: true # ... end Model: Rails
  20. class User < ActiveRecord::Base
 include Gravtastic before_destroy :yank_gems has_many :rubygems,

    through: :ownerships validates :name, presence: true # ... end Model: Rails
  21. Model: hanami entity >> user = User.new(id: 1) => #<User:0x007fa14d1e0528

    @attributes={:id=>1}> >> user.id => 1 >> user.id = 1 NoMethodError: undefined method `id=' for #<User:0x007fa14d1e0528 @attributes={:id=>1}> Did you mean? id
  22. Model: hanami entity >> user = User.new(id: 1) => #<User:0x007fa14d1e0528

    @attributes={:id=>1}> >> user.id => 1 >> user.id = 1 NoMethodError: undefined method `id=' for #<User:0x007fa14d1e0528 @attributes={:id=>1}> Did you mean? id
  23. class UserRepository < Hanami::Repository associations do has_many :books end def

    find_by_name(name) users # => ROM relation users.where(name: name).limit(1).one end end Model: hanami repository
  24. >> repo = UserRepository.new => #<UserRepository relations=[:users]> >> repo.find(1) =>

    #<User:0x007fa14d1a1fa8 @attributes={...}> Model: hanami repository
  25. Action test describe Web::Controllers::Board::Index do let(:action){ Board::Index.new } let(:params){ Hash[]

    } it 'is successful' do response = action.call(params) response[0].must_equal 200 end end
  26. Action test describe Web::Controllers::Board::Index do let(:action){ Board::Index.new } let(:params){ Hash[]

    } it 'is successful' do response = action.call(params) response[0].must_equal 200 end end
  27. Action test describe Web::Controllers::Board::Index do let(:action){ Board::Index.new } let(:params){ Hash[]

    } it 'is successful' do response = action.call(params) response[0].must_equal 200 end end
  28. TDD

  29. TDD