Skip to content

Instantly share code, notes, and snippets.

@Chryus
Chryus / gist:9592000
Last active August 29, 2015 13:57
Example of a before_filter that triggers a Devise method that redirects to login for all actions with exceptions. Rails 4/Devise authentication solution.
class EventsController < ApplicationController
before_filter :authenticate_user!, :except => [:show, :index, :api_by_name]
def index
@events = Event.all
end
###
@Chryus
Chryus / gist:9592133
Last active August 29, 2015 13:57
Container for user sign in/sign out
<div id="user_nav">
<% if user_signed_in? %>
Signed in as <%= current_user.email %>. Not you?
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% else %>
<%= link_to "Sign up", new_user_registration_path %> or <%= link_to "sign in", new_user_session_path %>
<% end %>
</div>
<div class="container">
<h2 class="form-signin-heading">Sign in</h2>
<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
<div><%= f.label :email %><br />
<%= f.email_field :email, :autofocus => true %></div>
###
<%= render "devise/shared/links" %>
</div>
@Chryus
Chryus / gist:9600004
Created March 17, 2014 14:18
Example of using conditional logic to check for admin status via Devise
<% if current_user.try(:admin?) %>
<%= link_to 'Edit', edit_event_path(event) %>
<%= link_to 'Destroy', event, confirm: 'Are you sure?', method: :delete %>
<% end %>
@Chryus
Chryus / gist:9600590
Created March 17, 2014 14:45
Example migration for admin column
class AddAdminToUsers < ActiveRecord::Migration
def change
add_column :users, :admin, :boolean, :default => false
end
end
@Chryus
Chryus / gist:d2ff4b2ba52ad8809fd0
Created February 13, 2015 17:41
run_callbacks example
def destroy(mode=:soft)
run_callbacks :destroy do
if mode == :hard
super()
else
update(deleted_at: Time.zone.now)
end
end
end
def avc_settings
config = {
'connectionstring' => 'localhost:3000',
'languagefile' => 'translations/en.xml',
'qualityurl' => '',
'maxRecordingTime' => 86400,
"useUserId" => "true",
'userId' => '',
'outgoingBuffer' => 0,
'playbackBuffer' => 1,
@Chryus
Chryus / upload.js.coffee
Last active October 21, 2015 15:23
upload form
$ ->
class SubmissionUploadForm
constructor: (form) ->
return if form.length == 0 # break unless form is present
@form = $(form)
@fileSelect = @form.find("#fileSelect")
@fileUpload = @form.find("#fileUpload")
@filesQueue = []
@setEvents()
// Cape Fear Leitmotif
// 1. Using Safari, go to http://stuartmemo.com/synth/
// 2. Enable Web Inspector - To do so, enable the “Show Develop menu in menu bar” setting found in Safari's preferences
// under the Advanced pane. You can then access Web Inspector through the Develop menu that appears in the menubar,
// or by pressing Command-Option-I. (see http://ow.ly/Y2Ae302Zn1C)
// 3. Open console (press Command-Option-I)
// 4. Copy and paste the code below
// 5. Press enter
# customizing as_json with standalone JBuilder DSL
def as_json(options={})
Jbuilder.encode do |json|
json.(self, :incident_address, :borough, :latitude, :longitude)
json.upvotes(self.upvotes.count)
json.upvoted_by(self.upvotes.pluck(:user_id))
end
end