Skip to content

Instantly share code, notes, and snippets.

View nuwansh's full-sized avatar
:electron:

Sameera Hettiarachchi nuwansh

:electron:
View GitHub Profile
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
# Build multiple objects
# Association is Client has many projects
client = Client.new first_name: "Bob"
# Add more projects
client.build [{ name: "Project1"}, { name: "Project2" }]
@nuwansh
nuwansh / subdomain_authenticatable.rb
Created April 16, 2013 16:41
Custom devise Strategy
#require 'devise/strategies/authenticatable'
module SubdomainAuthenticatable
# Default strategy for signing in a user, based on his email and password in the database.
class CustomStrategy < Devise::Strategies::Authenticatable
def authenticate!
resource = valid_password? && mapping.to.joins(:company).where(authentication_hash.merge({:companies => {:subdomain => "test"}})).first
return fail(:not_found_in_database) unless resource
if validate(resource){ resource.valid_password?(password) }
@nuwansh
nuwansh / destroy.rb
Created March 25, 2012 04:34
Destroy action [rails3]
def destroy
@object.destroy
respond_to do |format|
format.html {
flash[:notice] = "Deleted object."
redirect_to(objects_path)
}
format.xml { head :ok }
format.json { head :ok }
@nuwansh
nuwansh / create_object.rb
Last active October 2, 2015 05:58
Create Method
@object = object.new(params[:object])
respond_with(@object) do |format|
if @object.save
format.html {
flash[:notice] = "Created a new object successfully."
redirect_to objects_path
}
format.json { render :json => object.find(@object).to_json, :status => :created, :location => @object }
else
@nuwansh
nuwansh / slug_convert.rb
Created March 20, 2012 12:52
Takes the slug, downcases it and replaces non-word characters with a -
# Takes the slug, downcases it and replaces non-word characters with a -.
# Feel free to override this method if you'd like different slug formatting.
def normalize_slug_url
return if self.name.blank?
s = ActiveSupport::Multibyte.proxy_class.new(self.name).normalize(:kc)
s.downcase!
s.strip!
s.gsub!(/[^a-z0-9\s-]/, '') # Remove non-word characters
s.gsub!(/\s+/, '-') # Convert whitespaces to dashes
s.gsub!(/-\z/, '') # Remove trailing dashes
@nuwansh
nuwansh / convert_date.rb
Created March 20, 2012 11:19
Convert timestamp to normal format
def convert_date(date_string)
if date_string.present?
date = Time.parse(date_string.to_s)
date.strftime("%b %d, %Y")
end
end
@nuwansh
nuwansh / dabblet.css
Created December 31, 2011 09:34
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
/*background: #f06;
background: linear-gradient(45deg, #f06, yellow);
min-height:100%;*/
div {
background-color: red; float: left; width: 100px;
-webkit-animation-duration: 3s;
@nuwansh
nuwansh / dabblet.css
Created December 31, 2011 09:22
The first commented line is your dabblet’s title
/**
* The first commented line is your dabblet’s title
*/
background: #f06;
background: linear-gradient(45deg, #f06, yellow);
min-height:100%;