Skip to content

Instantly share code, notes, and snippets.

View Marchino's full-sized avatar

Marco Crepaldi Marchino

View GitHub Profile
@Marchino
Marchino / import.rb
Created September 29, 2011 13:47
import stuff from a csv with first row containing model's attributes
keys = []
CSV.foreach("/path/to/stuff/to/import.csv") do |row|
keys = row and first = false if first
ModelName.create(Hash[*keys.each{|k| k.strip!}.zip(row.each{|k| k.strip!}).flatten]) unless first
end
end
require 'spec_helper'
describe "Question", :search => true do
before(:each) do
@buyer = Buyer.first
@item = RareBook.first
host! 'mmplus.dev'
Capybara.app_host = "http://mmplus.dev"
reset_email
require 'spec_helper.rb'
describe "Phone call", :search => true do
it "should be created without user logged in" do
email_sent_until_now = email_count
item = RareBook.last
visit send("new_rare_book_phone_call_path", :title => item.to_url_param, :id => item.id)
fill_in "phone_call_name", :with => "User name"
fill_in "phone_call_phone_number", :with => "1234567890"
MM_ITEMS.each do |item_type|
scope "#{item_type.to_s.pluralize}" do
# index items of item_type
match "/" => "items/#{item_type.to_s.pluralize}#index", :as => "#{item_type.to_s.pluralize}"
# show item
match ":title/:id" => "items/#{item_type.to_s.pluralize}#show", :as => "#{item_type.to_s}"
# shipping costs for item
get ":title/:id/shipping_costs" => "items/#{item_type.to_s.pluralize}#shipping_costs", :as => "#{item_type.to_s}_shipping_costs"
# question for item
get ":title/:id/new_question" => "items/#{item_type.to_s.pluralize}#new_question", :as => "new_#{item_type.to_s}_question"
module Mmplus
module Controllers
module Search
extend ActiveSupport::Concern
included do
before_filter :fix_params
def do_search(search_params, classes = nil)
q = search_params
class Items::RareBooksController < Items::ItemsController
include Mmplus::Controllers::Items
end
module Mmplus
module Controllers
module Items
extend ActiveSupport::Concern
included do
before_filter :set_instance
def index
do_search(params[:search] || {}, @instance)
end
@Marchino
Marchino / mmplus.rb
Created September 9, 2011 09:55
Module inclusion
module Mmplus
module Models
autoload :Items, 'mmplus/models/items.rb'
autoload :Askable, 'mmplus/models/askable.rb'
autoload :Profile, 'mmplus/models/profile.rb'
autoload :ShippingCosts, 'mmplus/models/shipping_costs.rb'
autoload :BookshopConstraints, 'mmplus/models/bookshop_constraints.rb'
end
module Controllers
autoload :Search, 'mmplus/controllers/search.rb'
@Marchino
Marchino / core_extensions.rb
Created July 29, 2011 13:47
Search with squeel
class String
def to_query_params
words = []
self.split(/\W/).each do |word|
words << "%#{word}%"
end
return words
end
end
@Marchino
Marchino / wwwt.rb
Created July 18, 2011 09:46
what's wrong with this?
class ItemImage < ActiveRecord::Base
belongs_to :item, :polymorphic => true
mount_uploader :image, ImageUploader
scope :the_cover, where(:cover => true)
end
class Book < ActiveRecord::Base
has_many :item_images, :as => :item
end