Skip to content

Instantly share code, notes, and snippets.

I am attesting that this GitHub handle thenapking is linked to the Tezos account tz2FuyRtdMA8raUUuTXQY4xQjxp2dtkD7qRu for tzprofiles
sig:spsig17aZPt4tvaG5P99h38w9Xg6qXZrbykX5na6csiT8GQTTGhWDf3md2DSX4jhBMcJZ6DsZAtWSAPVg95yvHmQVgHMqR55Yxf
I am attesting that this GitHub handle thenapking is linked to the Tezos account tz1LdkNmdi3ij229JjkYeYQ5zCDhSjv8RC3R for tzprofiles
sig:edsigtwZjQrB1m3ygvqNdCoQSzvPy9rogdWmRnmJzcMifxwykXfiQXPY17X4Uc9QQkaGGL1JgzL3dBhiW89AWnzi3vpyTkfv7Jk
@thenapking
thenapking / Tezos
Created May 15, 2023 09:52
Tezos Verification
Tezos Signed Message: I am attesting that this GitHub handle thenapking is linked to the Tezos account tz1LdkNmdi3ij229JjkYeYQ5zCDhSjv8RC3R for tzprofiles
Tezos Signed Message: I am attesting that this GitHub handle thenapking is linked to the Tezos account tz1LdkNmdi3ij229JjkYeYQ5zCDhSjv8RC3R for tzprofiles
@thenapking
thenapking / basic_propane.rb
Created May 19, 2021 08:40
Basic Propane setup
#!/usr/bin/env jruby
require 'propane'
class Render < Propane::App
def settings
size 600, 600
end
def setup
@thenapking
thenapking / sti.rb
Last active May 18, 2021 09:01
Add Single Table Inheritance and a Tree Heirarchy to a model. See linked gist for an example of how to load data for a hierarchical table with STI https://gist.github.com/thenapking/3ccd4d20f3d9be74426ffa2354d44572
# models
class Location < ActiveRecord::Base
has_ancestry cache_depth: true, touch: true, orphan_strategy: :destroy
self.inheritance_column = :category
def self.categories
%w(World Country Division Region Area Store)
end
belongs_to :parent, class_name: "Location", foreign_key: "parent_id"
@thenapking
thenapking / cocoon.rb
Last active May 18, 2021 09:03
Setup a nested form with Cocoon
# Add to gemfile
gem 'cocoon'
# In application.js
//= require cocoon
# For a ticket, with associated ticket_comments
class Ticket < ActiveRecord::Base
has_many :ticket_comments, inverse_of: :ticket_detail
@thenapking
thenapking / model.rb
Last active May 18, 2021 09:03
ActiveRecord Validation and Hooks
# Examples of relationships
belongs_to :location, inverse_of: :people, class_name: "Store", required: true, touch: true
has_one :user, inverse_of: :person
has_many :user_locations, through: :user
has_one :person_flag, inverse_of: :person, autosave: true
has_many :pending_change_locations, through: :pending_changes, source: :locations
accepts_nested_attributes_for :ticket_comments, allow_destroy: true
@thenapking
thenapking / recursion.sql
Last active May 18, 2021 09:04
First format the data correctly by selecting each type of location into a view with a union. Start with the types at the top of the tree. Then use recursion to build the tree.
CREATE location_recursion AS
WITH recursion_tree AS
(
SELECT location_id, name, friendly_id, parent_id, manager_id, location_type, parent_location_type,
CAST(location_id AS VARCHAR(1000)) AS Path
FROM dbo.__Location_Children_vw
WHERE (parent_id IS NULL)
UNION ALL
SELECT t.location_id, t.name, t.friendly_id, t.parent_id, t.manager_id, t.location_type, t.parent_location_type,
@thenapking
thenapking / metaprogramming.rb
Last active May 18, 2021 09:04
Here's a neat example of metaprogramming using define_method rather than method_missing. Also a very neat use of each_with_index.
class Something
%w(zero one two three four five six seven eight nine).each_with_index do |w,i|
define_method(w) { perform i }
end
end