Skip to content

Instantly share code, notes, and snippets.

View manojmj92's full-sized avatar

Manoj M J manojmj92

View GitHub Profile
@manojmj92
manojmj92 / after_restart.rb
Last active November 23, 2016 11:44
Appsignal deploy markers - AWS Opsworks/Chef
revision_command = "git log -n 1 --pretty=format:'%H'"
revision = `#{revision_command}`
appsignal_token = <your appsignal push api key>
branch_name = <your branch>
server_name = <your server name>
rails_env = <your environment>
user = <deployment user>
appsignal_url = "https://push.appsignal.com/1/markers?api_key=#{appsignal_token}&name=#{server_name}&environment=#{rails_env}"
bash 'report deployment' do
# add an alias to your bash profile with this code as walk
# alias walk='ruby <your file path.rb>'
# Usage
# walk first
# walk next
# walk prev
case ARGV[0]
when "next"
rev_list = `git rev-list --children --all`
refs = rev_list.scan(/[a-z0-9]{40}(?= )/)
@manojmj92
manojmj92 / interactor_example.rb
Last active August 30, 2018 15:59
Interactor Example
# Definition of the Interactor
class AuthenticateUser
include Interactor
def call
if user = User.authenticate(context.email, context.password)
context.user = user
context.token = user.secret_token
else
@manojmj92
manojmj92 / interactor_new_attribute.rb
Created August 4, 2018 07:56
Interactor with New attribute
# Definition of the Interactor
class MyInteractor
include Interactor
def call
puts context.email # => my_email@reflektive.com
puts context.name # => my_name
context.new_attribute = 'some_random_value'
end
end
# Definition of the PORO
class MyInteractor
attr_reader :context
def initialize(email:, name:)
@context = {}
context[:email] = email
context[:name] = name
end
module InteractorValidations
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def requires(*attributes)
before do
attributes.each do |attribute|
# Definition of a BaseInteractor
class BaseInteractor
include Interactor
include ActiveModel::Validations
# For any class that inherits this class, a `before` hook is registered, which raises ArgumentError if the reqired parameters are not passed in during invocation.
def self.inherited(subclass)
subclass.class_eval do
def self.requires(*attributes)
# Without delegation
class MyInteractor
include Interactor
def call
if context.email.match(/^[a-z]+.\@reflektive\.com$/i)
puts "Welcome, #{context.name} from Reflektive!"
end
end
@manojmj92
manojmj92 / sandbox.rake
Created November 28, 2018 16:01
sanbox rake
desc 'Run rake tasks with sandboxing'
task sandbox: :environment do
# Start transaction
ActiveRecord::Base.connection.begin_transaction(joinable: false)
puts "🆒 Sandbox Mode: ON 🆒"
# Run your task
Rake.application.invoke_task(ARGV.delete_at(1))
desc 'destroys employee update'
task :employee_update_remove => :environment do
puts "Starting Migration"
records = EmployeeUpdate.last(3)
records.each do |record|
puts "Going to destroy record #{record.id}"
record.destroy
end
puts "Migration Ended"
end