Skip to content

Instantly share code, notes, and snippets.

View Electron-libre's full-sized avatar

Cedric Brancourt Electron-libre

View GitHub Profile
@Electron-libre
Electron-libre / application_controller.rb
Last active December 25, 2015 16:39
This show how to not write right management with Pundit.
class ApplicationController < ActionController::Base
# Includes Authorization mechanism
include Pundit
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
# Globally rescue Authorization Errors in controller.
@Electron-libre
Electron-libre / postgres_array_rails_migration.rb
Created October 16, 2013 13:22
This is an example of Postgres Array use with Rails 4.
class CreateRoles < ActiveRecord::Migration
def change
create_table :roles do |t|
t.string :activities, array: true, length: 30, using: 'gin', default: '{}'
t.timestamps
end
end
end
##
@Electron-libre
Electron-libre / rest_count.rb
Created July 25, 2013 10:41
HEAD request in Rails , used to send collection informations ...
module ApiController
def index
# ...
# How
if request.head?
@count = self.class.resource_model.count(user_context, format_filters(@filters)).content
head :ok, "Content-Range" => "#{self.class.resource_model.name} #{@offset}-#{@limit}/#{@count}"
else
1 cedric@Arch7 ~/Dev/humanIT/session_off (git)-[master] % bundle update :(
Fetching gem metadata from https://rubygems.org/.........
Fetching gem metadata from https://rubygems.org/..
Resolving dependencies...
Using rake (10.1.0)
Using i18n (0.6.4)
Using minitest (4.7.5)
Using multi_json (1.7.7)
Using atomic (1.1.10)
Using thread_safe (0.1.0)
[1] pry(#<Api::Openstc::DepartmentsController>)> request.session_options
=> {:path=>"/",
:domain=>nil,
:expire_after=>nil,
:secure=>false,
:httponly=>true,
:defer=>false,
:renew=>false,
:secret=>"b0509b8047175645ff907981d6772eaafec5306cedd24915236d39af143f",
:coder=>#<Rack::Session::Cookie::Base64::Marshal:0x0000000556b260>,
cedric@Arch7 ~/Dev/Siclic/barakafrites (git)-[rails_4] % rails s
=> Booting WEBrick
=> Rails 4.0.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Ctrl-C to shutdown server
[2013-07-04 15:46:03] INFO WEBrick 1.3.1
[2013-07-04 15:46:03] INFO ruby 2.0.0 (2013-06-27) [x86_64-linux]
[2013-07-04 15:46:03] INFO WEBrick::HTTPServer#start: pid=7937 port=3000
DEPRECATION WARNING: You didn't set config.secret_key_base. Read the upgrade documentation to learn more about this new config option. (called from service at /home/cedric/.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/webrick/httpserver.rb:138)
@Electron-libre
Electron-libre / openerp.rb
Created June 18, 2013 12:21
Ruby's dynamic accessors demo. Enjoy Ruby power :)
module Openerp
def self.define_setter(attr)
class_eval("def self.#{attr}=(val) \n @@#{attr} = val \n end \n", __FILE__, __LINE__+1)
end
def self.define_getter(attr)
class_eval("def self.#{attr} \n @@#{attr} \n end \n", __FILE__, __LINE__+1)
end
describe 'for prepaid account' do
before(:each) do
history = {
'_3_days_ago' => {'debit' => 0.0, 'credit' => 0.0, 'balance' => 100.0},
'_2_days_ago' => {'debit' => 0.5, 'credit' => 10.0, 'balance' => 109.5},
'_1_days_ago' => {'debit' => 2.0, 'credit' => 0.0, 'balance' => 107.5},
'_0_days_ago' => {'debit' => 0.0, 'credit' => 2.5, 'balance' => 110.0}
}
@in_account = Fabricate(:in_account, :current_amount => 100.0)
@Electron-libre
Electron-libre / reversement.rb
Created August 4, 2011 13:14
Script reversement v1
accounts = Account.where(:ss7 => true).map { |a| [a.customer, a.code] }
report = []
accounts.each do |account|
account_report = []
account_count = []
Journal.where('account' => account[1], 'date' => /2011-07/).each do |journal|
account_report << journal.billing_entries.sum(:duration)
account_count << journal.billing_entries.where(:duration.gt => 0.0).count
end
report << [account[0], account[1], account_report.compact.sum, account_count.sum]
module CallLog::Replay
# Class methods for CallLog
module ClassMethods
def replay_for(object, timestamp=Time.now.utc, kind='all')
case object.class
when Account
self.replay_account_from(object, timestamp, kind)
end
end