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
| $ time ruby rspec.rb && time ruby minispec.rb && time ruby minitest.rb | |
| ......................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................... |
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
| # lib/tasks/assets.rake | |
| namespace :assets do | |
| task :precompile_if_needed do | |
| require_relative '../assets_version' | |
| next unless AssetsVersion.needs_precompile? | |
| Rake::Task["assets:precompile"].invoke | |
| AssetsVersion.current.save_to_yaml | |
| 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 'delegate' | |
| class NumberDecorator < SimpleDelegator | |
| def in_dollars | |
| "$#{to_s}" | |
| end | |
| def in_euros | |
| "€#{to_s}" |
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
| #include "ruby.h" | |
| #define TO_BIGNUM(x) (FIXNUM_P(x) ? rb_int2big(FIX2LONG(x)) : x) | |
| static VALUE zero = INT2NUM(0); | |
| static VALUE one = INT2NUM(1); | |
| static VALUE c_mod_pow(VALUE self, VALUE exp, VALUE mod){ | |
| VALUE result = one; |
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
| [1] pry(main)> rating = Rating.new | |
| => #<Rating id: nil, comment: nil> | |
| [2] pry(main)> erb = ERB.allocate | |
| => #<ERB:0x007f841b98ba10> | |
| [3] pry(main)> proxy = ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy.new(erb, :result) | |
| => #<ActiveSupport::Deprecation::DeprecatedInstanceVariableProxy:0x3fc20dca5a08> | |
| [5] pry(main)> erb.instance_variable_set :@src, "User.steal_all_passwords;" |
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
| # Returns whether a can be split in max_blocks with a | |
| # max sum equal or lower to max_sum | |
| def can_split_in_blocks?(a, max_sum, max_blocks) | |
| n_blocks = 0 | |
| current_block_sum = 0 | |
| a.each_with_index do |elm, i| | |
| if current_block_sum + elm <= max_sum && i > 0 | |
| # Use the current block |
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
| # app/models/concerns/account_scoped.rb | |
| module AccountScoped | |
| extend ActiveSupport::Concern | |
| included do | |
| belongs_to :account | |
| before_validation :set_current_account, on: :create | |
| before_destroy :belongs_to_current_account |
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
| # app/controllers/concerns/authentication.rb | |
| # (Authorisation is different than Authentication and so it is handled in a different concern) | |
| module Authentication | |
| extend ActiveSupport::Concern | |
| included do | |
| before_action :authenticate | |
| helper_method :logged_in? | |
| 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
| # app/models/current.rb | |
| class Current < ActiveSupport::CurrentAttributes | |
| attribute :account, :person | |
| def person=(person) | |
| super | |
| self.account = person&.account | |
| 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
| class Invoice < ApplicationRecord | |
| include AccountScoped | |
| end | |
| class InvoicesController < ApplicationController | |
| def show | |
| @invoice = Invoice.in_current_account.find(params[:id]) | |
| end | |
| end |