Skip to content

Instantly share code, notes, and snippets.

View monfresh's full-sized avatar

Moncef Belyamani monfresh

View GitHub Profile
@monfresh
monfresh / swipe_to_delete.rb
Created August 8, 2012 19:17
Custom Calabash iOS step definition to swipe to delete all cells in a Table View
Then /^I swipe to delete all cells in "([^\"]*)"$/ do |orientation|
total_rows = query("tableView index:0",numberOfRowsInSection:0) #this returns an array
end_of_range = total_rows[0] - 1
(0..end_of_range).each do
scroll_to_row "tableView index:0", 0
sleep(STEP_PAUSE)
macro %Q[I swipe on cell number 1 in "#{orientation}"] #requires this custom step definition: https://gist.github.com/3298026
sleep(STEP_PAUSE)
touch "TableViewCellDeleteConfirmationControl"
end
@monfresh
monfresh / swipe_a_cell.rb
Created August 8, 2012 19:49
Custom step definition for Calabash iOS to swipe a cell in any orientation
Then /^I swipe on cell number (\d+) in "([^\"]*)"$/ do |index, orientation|
index = index.to_i
screenshot_and_raise "Index should be positive (was: #{index})" if (index<=0)
if orientation == "landscape"
swipe("up", {:query => "tableViewCell index:#{index-1}"})
elsif orientation == "portrait"
swipe("right", {:query => "tableViewCell index:#{index-1}"})
end
sleep(STEP_PAUSE)
end
@monfresh
monfresh / swipe_to_delete_and_check_cell.rb
Created August 9, 2012 21:20
Calabash iOS step that verifies that all cells have been deleted after swiping
def check_cell(prefix)
if @final_count[0] == 1
final_cell = query "TableViewCell index:0"
if final_cell.first["class"] == "UITableViewCell"
sleep(STEP_PAUSE)
else
screenshot_and_raise "#{prefix} of the cells were deleted!"
end
else
screenshot_and_raise "#{prefix} of the cells were deleted!"
@monfresh
monfresh / extract_tld_from_url.rb
Created May 14, 2013 01:39
Ruby script to extract the TLD from a URL. This can be useful for validating a URL.
urls = ["http://brigade.codeforamerica.org/index.html", "brigade.codeforamerica.org", "http://codeforamerica.org", "codeforamerica.org/index.html", "brigade.codeforamerica.org/index.html"]
urls.each do |url|
# clean up the URLs so they all start off with the same format
if url.include?("http://")
url.gsub!("http://","")
end
# we're only interested in the part that comes before the first slash,
# so we split the string into the part before the slash, and the part after the slash
a = url.split("/")
@monfresh
monfresh / gist:5663004
Created May 28, 2013 14:07
README.rdoc generated by Rails Apps Composer
== Welcome to Rails
Rails is a web-application framework that includes everything needed to create
database-backed web applications according to the Model-View-Control pattern.
This pattern splits the view (also called the presentation) into "dumb"
templates that are primarily responsible for inserting pre-built data in between
HTML tags. The model contains the "smart" domain objects (such as Account,
Product, Person, Post) that holds all the business logic and knows how to
persist themselves to a database. The controller handles the incoming requests
@monfresh
monfresh / gist:5663043
Created May 28, 2013 14:13
Defaults file for Rails Apps Composer for a basic Rails API app that uses MongoDB and Mongoid
recipes:
- setup
- readme
- gems
- testing
- controllers
- routes
- extras
prefs:
@monfresh
monfresh / gist:5816579
Last active December 18, 2015 17:09
Rails controller create action for Mongoid embedded_in model. What is the right way to test the post :create action in this situation?
# app/models/user.rb
class User
include Mongoid::Document
embeds_many :api_applications
...
end
# app/models/api_application.rb
class ApiApplication
include Mongoid::Document
@monfresh
monfresh / gist:9009832
Last active August 29, 2015 13:56
Is there a way to disable eager loading if the query doesn't return any results?
# Models
class Post < ActiveRecord::Base
  has_many :comments
end

class Comment < ActiveRecord::Base
  belongs_to :post
end
while getopts ":a:o:" opt; do
case $opt in
a)
echo "Getting ready to set environment variables for $2"
echo "Setting CANONICAL_URL"
heroku config:set CANONICAL_URL=$2.herokuapp.com --app $2
echo "Setting DOMAIN_NAME"
heroku config:set DOMAIN_NAME=herokuapp.com --app $2
@monfresh
monfresh / controller_refactor.rb
Last active June 10, 2016 14:11
Refactor exercise
def update
# reset attempt count if user is no longer locked out
unless resource.otp_time_lockout? || resource.second_factor_locked_at.nil?
resource.update(second_factor_attempts_count: 0, second_factor_locked_at: nil)
end
if resource.authenticate_otp(params[:code].strip)
warden.session(resource_name)['need_two_factor_authentication'] = false
sign_in resource_name, resource, bypass: true
set_flash_message :notice, :success