This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module StrongAdmin | |
| extend ActiveSupport::Concern | |
| def initialize | |
| @instance_name = active_admin_config.resource_class.name.underscore.to_sym | |
| @klass = active_admin_config.resource_class | |
| super | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Autogenerated C code from ruby code: | |
| a = 1 | |
| lambda { | |
| puts a | |
| }.call | |
| */ | |
| // This is the compiled lambda |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Kernel | |
| def with(object, &block) | |
| object.instance_eval &block | |
| end | |
| end | |
| # Simple way to use it | |
| with "Ruby Rocks!" do | |
| puts self | |
| puts size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Klass | |
| def self.class_method ; end | |
| end | |
| class Klass | |
| class << self | |
| def class_method ; end | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Kernel | |
| def companion(klass, &block) | |
| klass.instance_eval(&block) | |
| # or: klass.singleton_class.class_eval(&block) | |
| end | |
| end | |
| class Cat | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Add RVM to PATH for scripting | |
| [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session. | |
| __rvm_project_rvmrc |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ActiveRecordGroupCount | |
| extend ActiveSupport::Concern | |
| module ExtensionMethods | |
| def count(*args) | |
| scope = except(:select).select("1") | |
| query = "SELECT count(*) AS count_all FROM (#{scope.to_sql}) x" | |
| ActiveRecord::Base.connection.execute(query).first.try(:[], "count_all").to_i | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'java' | |
| require 'stringio' | |
| class ProcessBuilderWrapper | |
| class Status | |
| def initialize(status) | |
| @status = status | |
| end | |
| def success? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person < ActiveRecord::Base | |
| has_many :photos | |
| has_one :display_photo, class_name: "Photo" | |
| end | |
| # WARNING: This only appears to work properly! | |
| people = Person.all.includes(:display_photo) | |
| people.each { |person| puts person.display_photo.id } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Person < ActiveRecord::Base | |
| has_many :photos | |
| has_one :display_photo, -> { | |
| self.select_values = ["DISTINCT ON(photos.person_id) photos.*"] | |
| order('photos.person_id') | |
| }, class_name: "Photo" | |
| end | |
| # Polymorphic association example | |
| class Person < ActiveRecord::Base |