Skip to content

Instantly share code, notes, and snippets.

@andrewhaines
andrewhaines / README.md
Last active October 20, 2019 09:21
Rails UJS toggling strategy

Rails UJS toggling strategy

For 'has_many :through' associations

This strategy can be used for creating / removing associations between two objects in a Rails app using Ajax / UJS. For example, setting up tag associations (i.e., one tag name applied to many individual posts) or favorites (i.e., many users saving personal favorites against global objects).

It's designed to work in a list / table format, where you're creating and removing associations in a single page format. To initiate, make sure the list / table partial for each "owner" item contains the content from _item-partial.html.erb. A similar setup should also work in non-list / table formats, as long as each instance of the owner artifact includes the classes and data attributes required by the JavaScript partials.

For boolean toggling

@andrewhaines
andrewhaines / .env
Last active October 22, 2018 20:10
Using Duo MFA with Devise in a Rails app
DUO_INT_KEY=your_identity_key_from_duo
DUO_SECRET_KEY=your_secret_key_from_duo
DUO_APP_KEY=your_secret_string # Generatie with SecureRandom.gen_random(40)
DUO_HOST=api-123abc.duosecurity.com
@andrewhaines
andrewhaines / analyticsreportingv4.rb
Last active July 6, 2023 08:50
Using Google Analytics Reporting API v4 in Rails with Oauth2
# This summary shows how to set up a basic request to Google's Analytics Reporting API v4 from a Rails app.
# Gemfile
gem 'google-api-client', # v. 0.11
gem 'omniauth-google-oauth2' # v. 0.4.1 with Devise
# In a model or controller somewhere...
analytics = Google::Apis::AnalyticsreportingV4::AnalyticsReportingService.new
analytics.authorization = current_user.token # See: https://github.com/zquestz/omniauth-google-oauth2