Pages

2022年8月5日星期五

Ruby on Rails FAQ

 1. Keyword delegate pada Model

Contoh kasus penggunaan keyword delegate


class University < ActiveRecord::Base

  has_many :lecturers

end


class Lecturer < ActiveRecord::Base

  belongs_to :university

end


[1] pry(main)> my_lecturer = Lecturer.find('459352183')

=> “Agung Supriadi”

[2] pry(main)> my_lecturer.university.abbreviated_name

=> “BINUS”

[9] pry(main)> my_lecturer.university.address

=> “Jl. Kebon Jeruk Raya No. 27, Kebon Jeruk”


Jika kita menggunakan keyword delegate, maka kita bisa mengakses lebih cepat:

class Lecturer < ApplicationRecord

  belongs_to :university

  delegate :abbreviated_name, :address, to: :university

end


[10] pry(main)> my_lecturer.abbreviated_name

=> “BINUS”


[11] pry(main)> my_lecturer.address

=> “Jl. Kebon Jeruk Raya No. 27, Kebon Jeruk”


Bagaimana jika my_lecturer pun punya address? maka kita bisa gunakan prefix pada delegate


class Lecturer < ApplicationRecord

  belongs_to :university

  delegate :abbreviated_name, :address, to: :university, prefix: true


  def address

    "this is address of the lecturer"

  end

end


[21] pry(main)> my_lecturer.address

=> "this is address of the lecturer"


[22] pry(main)> my_lecturer.university_address

=> “Jl. Kebon Jeruk Raya No. 27, Kebon Jeruk”


Atau menggunakan custom prefix

class Lecturer < ApplicationRecord

  belongs_to :university

  delegate :abbreviated_name, :address, to: :university, prefix: :campus


  def address

    "this is address of the lecturer"

  end

end


[22] pry(main)> my_lecturer.campus_address

=> “Jl. Kebon Jeruk Raya No. 27, Kebon Jeruk”

2022年6月16日星期四

Variable Shadowing & 'this' Keyword

 Take a look at this piece of code




For those who remember boiler plate code, this code will looks strange because it hasn't 'this' keyword. But this code works fine


this keyword only needed and become obligatory when facing variable shadowing.



This is variable shadowing. The constructor method will never be able to modify class attributes, because attributes name is the same with parameter name (the variable name overridden). Therefore you need to use this keyword, so the compiler could distinguish the address belongs to the parameter, and which one belongs to the attributes

Note: When calling instance method, people usually use this keyword. It is not necessary actually


2022年6月14日星期二

HTTPS for Java Spring Boot

Using Lets Encrypt is not suitable for spring boot I think, because its certbot doing verification with static content, while you serving single jar file in your system (no /static folder). You should modify some project structure, and it sucks

Instead, you should go to ZeroSSL, choose verification via CNAME record instead of HTTP upload

Got your certificate!




After getting certificate, you should install it
in case you have Tomcat server, your Spring boot code will have server.xml
But mine is not, maybe because it is standalone jar file

so to install the cert, follow this guide:
https://medium.com/@marianfurdui/generate-a-java-keystore-from-lets-encrypt-for-java-web-spring-or-spring-boot-applications-bf07408158ef

from that guidance, you will get file ending in .p12

after which, you should set it up on your application.properties like so


souce: http://www.heydari.be/2016/05/14/Spring-Boot-Application-Secured-by-Lets-Encrypt-Certificate.html
dont forget to match your server.ssl.keyAlias with the -name you created previously (maybe its root or keybin or tomcat)



That was I long journey. I started at 05.30 and finish at 09.00. About 3,5 hours to get my instance up and run with HTTPS