Skip to content

Instantly share code, notes, and snippets.

View abhishek0's full-sized avatar

Abhishek Sharma abhishek0

  • Snapstick Inc.
View GitHub Profile
@abhishek0
abhishek0 / application_helper.rb
Created September 21, 2013 14:41
Mailer view using helper throws error
module ApplicationHelper
def readable_bool val
val ? "Yes" : "No"
end
end
object @user
attributes *(@user.class.column_names - %w{id user_type parent_id stamp_id created_at updated_at password_digest access_token})
child :stamp => :contact do |stamp|
glue(stamp.contact) { attributes :first_name, :last_name, :email, :contact1, :contact2 }
end
node(:status) {|user| user.stamp.status }
raw_data = JSON.parse(response.body)
@data = Hash.new{|h,k| h[k]=Hash.new(&h.default_proc) }
raw_data.each do |u|
# u["id"] actually prints u itself??
id = u["id"], name = u["first_name"], l = u["locality"], c = u["city"]
@data[id]["first_name"] = name
@data[id]["locality"] = l
@data[id]["city"] = c
@data[id][u["status"]] = u["count"]
end
users = Arel::Table.new(:users)
stamps = Arel::Table.new(:stamps)
contacts = Arel::Table.new(:contacts)
children = users.alias
query = users.where(users[:user_type].eq('franchisee'))
query = query.join(stamps).on(users[:id].eq(stamps[:stampable_id]))
query = query.join(contacts).on(contacts[:id].eq(stamps[:contact_id]))
query = query.join(children).on(users[:id].eq(children[:parent_id]))
query = query.join(stamps).on(children[:id].eq(stamps[:stampable_id]))
@abhishek0
abhishek0 / ar_q.rb
Last active December 21, 2015 01:19
User.includes(stamp: [:contact]).where(:user_type => 'franchisee').joins(:stamp).joins(:children).group("users.id, stamps.status").count("stamps.status")
require 'spec_helper'
describe UsersController do
render_views
describe 'franchisee actions on users' do
before :each do
fr = FactoryGirl.build(:user, user_type: 'franchisee')
fr.set_stamp(FactoryGirl.build(:stamp), FactoryGirl.build(:contact, email: 'franchisee@example.com'))
fr.save
require 'spec_helper'
require 'faker'
describe Stampable do
it "throws error if stamp and contact is unset" do
expect { FactoryGirl.create(:user) }.to raise_error
end
it "can be saved if contact and stamp were set" do
user = FactoryGirl.build(:user)
module Contactable
def self.set_contact(contact)
@contact = Contact.find_by_email(contact[:email])
@contact = Contact.create!(contact) unless @contact.exists?
end
def self.get_contact
@contact
end
end
class CreateInquiries < ActiveRecord::Migration
def change
create_table :inquiries, id: false do |t|
t.primary_key :id, :uuid, :default => 'uuid_generate_v1()'
t.belongs_to :contact
t.string :inquiry_type
t.hstore :metadata
t.string :locality
t.string :city
t.uuid :added_by
{
"user" : {
"first_name": "John",
"last_name": "Doe",
"email": "a@a.com",
"mobile": "9876545678",
"locality": "Goregaon",
"city": "Mumbai",
"user_type": "advisor",
}