Skip to content

Instantly share code, notes, and snippets.

@devkinoti
devkinoti / account.rb
Created June 29, 2016 05:52
accounts model for multi tenant app
class Account < ActiveRecord::Base
belongs_to :owner, class_name: "User"
RESTRICTED_SUBDOMAINS = %w(www)
validates :owner, presence: true
validates :subdomain, presence: true,
uniqueness: { case_sensitive: false },
format: {with: /\A[\w\-]+\Z/i, message: "contains invalid characters not allowed for subomain"},
@devkinoti
devkinoti / accounts_controller.rb
Created June 29, 2016 05:51
basic accounts controller for multi tenant app
class AccountsController < ApplicationController
skip_before_filter :authenticate_user!,only: [:new,:create]
def new
@account = Account.new
@account.build_owner
end
def create
@account = Account.new(account_params)
if @account.valid?
@devkinoti
devkinoti / application_controller.rb
Created June 29, 2016 05:50
application controller for multi-tenant app
class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :load_schema, :authenticate_user!
private
def load_schema
Apartment::Tenant.switch!("public")
#!/usr/bin/env ruby
# encoding: utf-8
Dir[File.join(ARGV[0], '**', '*.{rb,rake}')].each do |file|
lines = File.open(file).readlines
next if lines[0].match(/#\s+(?:en)?coding:\s+utf-8/i)
puts "Adding encoding magic comment to #{file}"
File.open(file, 'w') do |io|
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')
#Model
#note however that its easier to use this with the expect syntax
@user.should have(1).error_on(:username) # Checks whether there is an error in username
@user.errors[:username].should include("can't be blank") # check for the error message
#Rendering
gem 'database_cleaner', group: :test
# RSpec's subject method, both implicitly and explicitly set, is useful for
# declaratively setting up the context of the object under test. If you provide a
# class for your describe block, subject will implicitly be set to a new instance
# of this class (with no arguments passed to the constructor). If you want
# something more complex done, such as setting arguments, you can use the
# explicit subject setter, which takes a block.
describe Person do
context "born 19 years ago" do
subject { Person.new(:birthdate => 19.years.ago }
it { should be_eligible_to_vote }
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
=Navigating=
visit('/projects')
visit(post_comments_path(post))
=Clicking links and buttons=
click_link('id-of-link')
click_link('Link Text')
click_button('Save')
click('Link Text') # Click either a link or a button
click('Button Value')