Skip to content

Instantly share code, notes, and snippets.

View coolprobn's full-sized avatar

Prabin Poudel coolprobn

View GitHub Profile
@coolprobn
coolprobn / application_helper.rb with rails console code
Last active December 2, 2024 12:37
Test view helper methods without having to render the whole view in Rails using rails console
# E.g. You have this code to format float values in your view
module ApplicationHelper
def format_number(value, precision: 4)
number_with_precision(value, precision:, strip_insignificant_zeros: true, delimiter: ',')
end
end
# You can test the method format_number() using `helper.method_name` in rails console
$ rails c
=> helper.format_number(120.0019)
@coolprobn
coolprobn / clipboard_controller.js
Created October 8, 2024 04:26
Copy to Clipboard with Rails, StimuluJS & TailwindCSS + DaisyUI
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["source", "checkbox"]
copy() {
const textToCopy = this.sourceTarget.innerText
if (!this.checkboxTarget.checked && this.isManuallyTriggeredClick) {
this.isManuallyTriggeredClick = false
@coolprobn
coolprobn / date_in_preferred_time_zone.rb
Created June 19, 2024 15:18
Convert any DateTime to the timezone you prefer while still keeping the same date and time - Ruby on Rails
def date_in_danish_time(date_time)
date_array =
date_time.strftime("%Y %m %d").split.map { |string| Integer(string, 0) }
danish_date = Date.new(*date_array).in_time_zone("Copenhagen")
# extracting offset from the date will help us in also taking care of Daylight Saving
danish_date_offset = danish_date.formatted_offset
date_time_array =
date_time
.strftime("%Y %m %d %H %M %S")