Skip to content

Instantly share code, notes, and snippets.

View alexvbush's full-sized avatar

Alex Bush alexvbush

View GitHub Profile
@alexvbush
alexvbush / articles_controller.rb
Last active August 29, 2015 14:03
Second iteration of Articles controller.
class Api::Private::ArticlesController < Api::Private::BaseController
include ArticlesSearch
before_filter :find_or_create_article, except: [:index]
PAGE_SIZE = 20
def index
@articles = Article.scoped
total_pages = 0
page = params[:page] ? params[:page] : 1
@alexvbush
alexvbush / articles_search.rb
Created July 14, 2014 01:22
Articles Search mixin.
module ArticlesSearch
def articles_search(search_criteria, state = nil)
@articles = Article.includes(:categories).where('title LIKE ? OR categories.name LIKE ?', "%#{search_criteria}%", "%#{search_criteria}%")
if state
@articles = @articles.where(state: state)
end
@articles
@alexvbush
alexvbush / articles_controller.rb
Last active August 29, 2015 14:03
First naive implementation of Articles controller.
class Api::Private::ArticlesController < Api::Private::BaseController
before_filter :find_or_create_article, except: [:index]
def index
@articles = Article.scoped
if params[:state] == Article::PENDING
@articles = Article.pending.order(:published_at)
elsif params[:state] == Article::APPROVED
@articles = Article.approved.order('approved_at DESC')
@alexvbush
alexvbush / article.rb
Last active August 29, 2015 14:03
First naive implementation of Article model.
class Article < ActiveRecord::Base
include Categorizable
APPROVED = 'approved'
PENDING = 'pending'
REJECTED = 'rejected'
attr_accessible :approved_at, :url, :body, :image, :rating, :state, :title
validates :url, :uniqueness => true
@alexvbush
alexvbush / CollectionTest.java
Created November 17, 2013 07:14
An example of a custom collection class that you can use to iterate over your own collection object using for_each loop. The key ingredient is Iterable, and Iterator interfaces implemented in CustomCollectionWrapper class.
/**
*
*/
/**
* Smart Cloud, Inc. Nov 14, 2013.
* @author Alex Bush
*
*/
public class CollectionTest {
@alexvbush
alexvbush / AndroidWeakRefExample.java
Last active July 6, 2016 18:57
An example of how to store an Android Context object in a weak reference to avoid unexpected retain loops in adapters and such.
public class ExampleWeak {
private WeakReference<Context> weakContext;
public ExammpleWeak(Context context) {
this.weakContext = context;
}
public Context getContext() {
return weakContext.get();
}
FactoryGirl.define do
factory :post do
sequence(:post_title) {|n| "Post#{n}"}
post_date { Time.now - 10.days }
post_date_gmt { (Time.now - 10.days).gmtime }
post_modified { Time.now - 10.minutes }
post_modified_gmt { (Time.now - 10.minutes).gmtime }
post_status {"publish"}
factory :artist, :class => 'Artist' do
@alexvbush
alexvbush / UIAlertViewPopupExample.m
Created January 27, 2012 07:26
iOS UIAlertView popup
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Some Title Here"
message:@"And some description message here"
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles: nil] autorelease];
[alert show];
@alexvbush
alexvbush / calculate_folder_size.rb
Created September 23, 2011 11:35
Recursively calculates the size of a folder(path to folder is the first input in terminal). Found other solutions using system calls but decide to write my own.
if ARGV.count != 1 || !File.directory?(ARGV[0])
p 'Please specify a folder.'
exit
end
def calculate_size(file)
#p 'calculating ' + file
if File.directory? file
@alexvbush
alexvbush / ActivityImplementsOnClickListener.java
Created September 9, 2011 09:38
Android various definition of OnClickListeners.
public class AndroidOnClickDefinitionInXMLActivity extends Activity implements OnClickListener {
private Button buttonWithActivityAsListener;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);