Skip to content

Instantly share code, notes, and snippets.

View stevepolitodesign's full-sized avatar
💎
return profile.status;

Steve Polito stevepolitodesign

💎
return profile.status;
View GitHub Profile
@stevepolitodesign
stevepolitodesign / turbo_tags_example.md
Last active June 5, 2024 13:33
Turbo Tags Example
# app/controllers/post/tags_controller.rb

class Post::TagsController < ApplicationController
  def new
    @tag = Tag.new
  end

  def create
 @tag = Tag.new(tag_params)
@stevepolitodesign
stevepolitodesign / Rails node_version error
Last active May 7, 2024 09:48
Rails node_version error
irb(main):001* ENV.fetch("NODE_VERSION") do
irb(main):002* `node --version`
irb(main):003* rescue
irb(main):004* "1.2.3"
irb(main):005> end
=> "1.2.3"
irb(main):006> exit
~/Desktop took 7s
❯ rails new node_demo \
@stevepolitodesign
stevepolitodesign / example.md
Last active April 15, 2022 21:59
Send secure messages in Rails. Work in progress.
# app/controllers/secret_messages_controller.rb
class SecretMessagesController < ApplicationController
  def create
    @secret_message_form = SecretMessageForm.new(secret_message_form_params)

    if @secret_message_form.valid?
      encrypted_content = encrypt_content(
        seed_phrase: params[:secret_message_form][:seed_phrase],
        password: params[:secret_message_form][:password],
@stevepolitodesign
stevepolitodesign / example.md
Last active March 16, 2022 15:35
Use with_indifferent_access to format a hash for reliable equality checks.
hash_one = { "name" => "Steve"}
# => {"name"=>"Steve"}
hash_two = { name: "Steve"}
# => {:name=>"Steve"}
hash_one == hash_two
# => false
hash_one.with_indifferent_access == hash_two.with_indifferent_access
# => true
@stevepolitodesign
stevepolitodesign / example.md
Created March 15, 2022 09:25
Rails destroy_by example

Before

Post.where("published_at < ?", 2.weeks.ago).destroy_all

After

Post.destroy_by("published_at &lt; ?", 2.weeks.ago)
@stevepolitodesign
stevepolitodesign / example.md
Created February 25, 2022 20:46
ActionDispatch::Cookies httponly option

Before

def remember(active_session)
  cookies.permanent.encrypted[:remember_token] = active_session.remember_token
end

After

@stevepolitodesign
stevepolitodesign / example.md
Last active February 17, 2022 21:47
Rails as_json API Example

Rails as_json API Example

Returns a hash representing a model and it's associated data. Useful for building an JSON API.

Associations

class User < ApplicationRecord
  has_many :injuries
  has_many :appointments, through: :injuries
@stevepolitodesign
stevepolitodesign / example.md
Last active September 23, 2023 15:30
Testing a Generator in a Rails Engine

Testing a Generator in a Rails Engine

Testing a generator in a Rails engine can result in unwanted modifications of files in the test/dummy directory.

# lib/generators/my_engine/install_generator.rb
module MyEngine
  module Generators
    class InstallGenerator < Rails::Generators::Base
      source_root File.expand_path("templates", __dir__)
@stevepolitodesign
stevepolitodesign / example.md
Created February 9, 2022 10:49
Testing a Rails generator

Generator

class DemoGenerator < Rails::Generators::Base
  source_root File.expand_path("templates", __dir__)

  def make_table
    generate "migration", "create_demos_table name:string"
  end
end
@stevepolitodesign
stevepolitodesign / example.md
Last active February 2, 2022 19:49
Raise error that need to be handled manually

Intentionally raise an error that needs to be handled manually

Sometimes there are cases when an actual human needs to intervene during edge cases.

# app/lib/manual_intervention_error.rb
class ManualInterventionError < StandardError
  def initialize(message = "An error occured that needs to be addressed manually")
    super
 end