Skip to content

Instantly share code, notes, and snippets.

View secretpray's full-sized avatar
🎯
Focusing

Aleksander secretpray

🎯
Focusing
View GitHub Profile
@secretpray
secretpray / advance-tooltip
Last active October 27, 2021 16:47
Advance Tooltip
Advance Tooltip
---------------
@secretpray
secretpray / Modded gravatar for User model.md
Last active March 15, 2023 18:31
Modded gravatar for User model (Tailwind 2.x)
  1. Gemfile (bundle install)
```ruby
gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'master' # optional!!! (only for use in seed.rb)
gem 'faraday' # optional!!! (only for use in seed.rb)
gem 'gravatar_image_tag', github: 'secretpray/gravatar_image_tag', branch: 'master'
gem 'hotwire-rails' # optional!!!
gem 'image_processing', '~> 1.2'
gem 'tailwindcss-rails', '~> 0.5.1' # optional!!!
```
@secretpray
secretpray / Check user_signed_in? and Is_author in Turbo Stream.md
Last active January 29, 2022 09:09
Check user_signed_in? and Is_author in Turbo Stream broadcasts
  1. Generate Stimulus controller
bin/rails g stimulus object_author
  1. app/javascript/controllers/object_author_controller.js
import { Controller } from '@hotwired/stimulus'

export default class extends Controller {
@secretpray
secretpray / Flash like tailwind with Stimulus (Rails 7).md
Last active December 18, 2022 08:29
Flash like tailwind with Stimulus (Rails 7)
app/views/layouts/application.html.erb
<!DOCTYPE html>
<html>
  <head>
    <title>Turboapp</title>
    <%= csrf_meta_tags %>
    <%= csp_meta_tag %>
@secretpray
secretpray / Migration_sample.md
Last active March 3, 2022 07:49
Rails Migration sample

Rails can generate a lot of things for you. Personally I use generate model often to quickly setup a new model, including test files and a database migration. In its simplest form it looks like this:

rails generate product

This will get you started with a naked Product model. To make things easier, you can also supply attribute names:

rails generate product name description:text 
@secretpray
secretpray / Setup Redis (macOS).md
Created February 4, 2022 11:36
Redis setup on Mac OS

type below:

brew update
brew install redis

To have launchd start redis now and restart at login:

brew services start redis
# app/views/messages/_message.html.erb
<%= message.decorate.formatted_for_room %>
#app/decorators/message_decorator.rb

class MessageDecorator < Draper::Decorator
 delegate_all
@secretpray
secretpray / Convert DB in Rails App.md
Last active March 3, 2022 08:53
Convert sqlite3 to PostgreSQL (and other DB) in Rails

Rails 6+

bin/rails db:system:change --to=postgresql

See list of other database type below:

rails db:system:change --to=mysql, 
rails db:system:change --to=sqlite3, 
@secretpray
secretpray / Rails routes.md
Last active March 12, 2022 10:14
Search in rails routes

Searching with Browser

You will have four outputs for each route. These outputs are the route name (if it exists), the HTTP verb, the url pattern and the routing parameters (if they are used). These four pieces of information are very useful in determining the full route including namespaces, what the components of the URI are, the action that will be performed when the endpoint is hit and what kind of parameters can be expected.

https://localhost:3000/rails/info/routes
@secretpray
secretpray / Infinity scroll.md
Last active June 26, 2022 09:05
Infinity scroll (Endless) on Ruby on Rails 7 Hotwire (Turbo)

Edition 1 simple (on Observer)

import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="infinite-scroll"
export default class extends Controller {

  initialize() {
    this.observer = new IntersectionObserver(entries => this.callback(entries))