Skip to content

Instantly share code, notes, and snippets.

View panSarin's full-sized avatar

Adam Piotrowski panSarin

View GitHub Profile
@panSarin
panSarin / form_object.rb
Created March 22, 2014 13:46
My first services + form objects
class FormObject
include ActiveModel::Model
def initialize(args={})
args.each do |k,v|
instance_variable_set("@#{k}", v) unless v.nil?
end
end
def save
@panSarin
panSarin / policy_service.rb
Created March 27, 2014 12:35
expectation doesnt work
#encoding: utf-8
class PolicyService < BaseService
attr_accessor :policy_form
def initialize(current_user, params, request)
super
@policy_form = PolicyForm.new(params[:policy].merge(current_user: @current_user, id: params[:id].present? ? params[:id] : nil))
end
def process_create
@panSarin
panSarin / tickets_controller_spec.rb
Created June 12, 2014 14:12
tickets_controller_spec_issue
require 'spec_helper'
describe TicketsController, type: :controller do
it 'should confirm ticket' do
body_of_test
end
end
@panSarin
panSarin / agency_scoped.rb
Last active August 29, 2015 14:02
I want to have scope on every model that select records only from current_user company
class AbstractModel < ActiveRecord::Base
scope :for_agency, lambda { |user|
where("agency_id = ?", user.agency_id)
}
end
#some example models
class Foo < AbstractModel
end
class Bar < AbstractModel
@panSarin
panSarin / client.rb
Last active August 29, 2015 14:04
company scope
class Client < ActiveRecord::Base
include CompanyScope
end
# and so on for each model
@panSarin
panSarin / clients_controller.rb
Last active August 29, 2015 14:04
controller default scopes
class ClientsController < CompanyScopedController
before_filter :set_client, only: [:show, :edit, :update, :destroy]
def index
@clients = clients_scoped.paginate(per_page: 20, page: params[:page])
end
private
def set_client
@panSarin
panSarin / application_policy.rb
Last active August 29, 2015 14:04
Pundit policies and our controllers
class ApplicationPolicy
# ... content generated by pundit (its about access for actions - we will discuss it in part 2 of that blogpost)
class Scope # our default scope for each model
attr_reader :user, :scope
def initialize(user, scope)
@user = user
@scope = scope
end
@panSarin
panSarin / bstokjs1.json
Last active August 29, 2015 14:13
bstok1.json
{ 'KR' : 'Angular and stuff', 'AP' : 'Jasmine', 'AC' : 'AexolGl'}
@panSarin
panSarin / bstokjs2.json
Created January 20, 2015 12:39
bstok2.json
{'XX' : 'YYY', 'ZZ': 'AAA'}
@panSarin
panSarin / MyRequests.js
Created January 20, 2015 12:51
Jasmine presentation source code - problem with response method
MyRequests.prototype.display_response = function(response){};
MyRequests.prototype.get_topics = function(bstokJS_stage) {
$.ajax({
type: 'GET',
url: 'https://gist.githubusercontent.com/panSarin/3d0e3080ee36e3b82c79/raw/8f496cfd22a766644b0ee834afb813ca1790c304/bstokjs'+bstokJS_stage+'.json',
contentType: "application/json; charset=utf-8",