Skip to content

Instantly share code, notes, and snippets.

View rishighan's full-sized avatar
🐈‍⬛
Focusing

Rishi Ghan rishighan

🐈‍⬛
Focusing
View GitHub Profile
// pasting relevant bits
$scope.createPost = function() {
$http({
method: 'POST',
url: '/db/createpost',
data: $scope.postFormModel,
headers: {'Content-Type': undefined},
}).then(function successCallback(data) {
console.log(data);
result = Rails.cache.fetch('google_analytics_api/#{cache_key}', expires_in: 2.hours) { client.execute(:api_method => analytics.data.ga.get, :parameters => parameters) }
class Pageviews < ActiveRecord::Base
#google analytics api shenanigans
require 'google/api_client'
require 'date'
include ReportingHelper
def self.getviews post
client, analytics, parameters = ReportingHelper.initclient
parameters = {
@rishighan
rishighan / posts_controller.rb
Created August 26, 2014 12:01
update action in posts controller
def update
respond_to do |format|
case params[:commit]
when "Save Draft"
@post.update_attribute(:is_draft, "yes")
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { head :no_content }
when "Update Post"
@post.update_attribute(:is_draft, "no")
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
@rishighan
rishighan / posts.js
Created August 24, 2014 14:55
Turbolinks and page:load
$(document).ready(function(){
setProjectHeroImage();
setDominantColor('#color-target');
});
$(document).on('page:load', setProjectHeroImage);
$(document).on('page:load', setDominantColor);
@rishighan
rishighan / loop.rb
Last active August 29, 2015 14:05
How to create a hash from a collection in the view ?
h =[]
@posts.each do |p|
p.categories.each do |cat|
h << {p.id => cat.title}
end
end
@rishighan
rishighan / console.rb
Last active August 29, 2015 14:05
How to check for existence of element in a collection
@log.last.categories
=> #<ActiveRecord::Associations::CollectionProxy [#<Category id: 1, title: "General", description: "Toto", created_at: "2014-07-25 19:09:17", updated_at: "2014-07-30 14:50:42">, #<Category id: 6, title: "Highlight", description: "Some shenanigans on the way to the graveyard.", created_at: "2014-08-12 15:56:42", updated_at: "2014-08-12 15:56:42">]>
@rishighan
rishighan / howiuseit.rb
Last active August 29, 2015 14:05
Constructing queries by merging scopes.
@post = Post.hero.group_by_category("include_with_hero", ["General"])
#Intended result is all posts that fall under the category General AND Hero
#I want this to work with multiple categories, so something like
@post = Post.hero.group_by_category("include_with_hero", ["General", "Foo", "Bar"])
#should return all posts that fall under General AND Foo AND Bar AND Hero categories
@rishighan
rishighan / Attachment.rb
Created July 30, 2014 11:54
Cocoon not saving all fields
class Attachment < ActiveRecord::Base
include Rails.application.routes.url_helpers
belongs_to :imageable
has_attached_file :picture,
:styles =>{ :medium => "660x",
:thumb => "150x"
},
:url => "/attachments/pictures/:style/:basename.:extension",
:path =>"#{Rails.root}/public/attachments/pictures/:style/:basename.:extension"
@rishighan
rishighan / _form.html.erb
Last active August 29, 2015 14:04
dropzone + paperclip multiple image upload issue.
<%= form_for(@post, html: {class:"dropzone"}) do |f| %>
<% if @post.errors.any? %>
<div class="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>