Skip to content

Instantly share code, notes, and snippets.

View swaathi's full-sized avatar
💃

Swaathi Kakarla swaathi

💃
View GitHub Profile
@swaathi
swaathi / files_controller.rb
Last active July 22, 2016 08:06
Preview files outside Public or Assets folder in Rails
# In your controller use the Rails in-built function, send_file to send the file to your browser for preview.
# Specify the filepath of your file in the send_file function.
def preview
send_file "#{Rails.root}/uploads/pictures/#{@file.name}"
end
@swaathi
swaathi / songs_controller.rb
Last active September 16, 2015 12:34
Elasticsearch with Searchkick gem
def search
@songs = Song.search(params[:query])
if request.xhr?
render :json => @songs.to_json
else
render :index
end
end
@swaathi
swaathi / index.html.erb
Created September 16, 2015 12:53
React Component
<p id="notice"><%= notice %></p>
<h1>Listing Songs</h1>
<%= react_component "SongsContainer", { songsPath: songs_path(:json), searchPath: search_path } %>
<%= link_to 'New Song', new_song_path %>
@swaathi
swaathi / _songs_container.js.jsx
Created September 16, 2015 17:55
Songs Container
var SongsContainer = React.createClass({
componentWillMount(){
this.fetchSongs();
},
fetchSongs() {
$.ajax({
url: this.props.songsPath,
var Songs = React.createClass({
render() {
var showSongs = (song) => <Song name={song.name} artist={song.artist} key={song.id}/>;
return <ul>{this.props.songs.map(showSongs)}</ul>;
}
});
var Song = React.createClass({
render () {
return (
<div>
<h4>{ this.props.artist } sang:</h4>
<p>{ this.props.name }</p>
</div>
)
}
});
var SongsSearch = React.createClass({
render () {
return (
<div>
<form ref="form" action={ this.props.searchPath } acceptCharset="UTF-8" method="get">
<p><input ref="query" name="query" placeholder="Search here." onChange={ this.props.submitPath } /></p>
</form>
<a href="#" onClick={ this.props.cancelPath }>Cancel</a>
</div>
@swaathi
swaathi / getNames.jsx
Created July 17, 2016 06:35
Convert an array into a comma seperated sentence.
getNames: function(people) {
var length = people.length;
if (length == 0) {
return "";
} else if (length == 1) {
return people[0].name;
} else if (length == 2) {
return people[0].name + " and " + people[1].name;
} else {
@swaathi
swaathi / time.rb
Last active September 26, 2016 04:26
Offset your time by timezone difference
class Time
# Adds an offset to your time
# So you can go from
# Fri, 22 Jul 2016 07:56:38 UTC +00:00
# To,
# Fri, 22 Jul 2016 13:26:38 UTC +00:00
#
# Example,
# t = Time.now.utc
# => Fri, 22 Jul 2016 07:56:38 UTC +00:00
@swaathi
swaathi / parser.rb
Last active December 25, 2016 11:18
ML Training Data
require 'fileutils'
require 'securerandom'
directory = File.expand_path File.dirname(__FILE__)
textfiles = "#{directory}/textfiles"
FileUtils::mkdir_p textfiles
Dir["#{directory}/*.scss"].each do |file|
file_content = File.new(file, "r").read
rgbas = file_content.scan(/rgba\(([0-9, ]*)/)