Skip to content

Instantly share code, notes, and snippets.

@bsylvain
bsylvain / formtastic.rb
Created December 20, 2012 10:45
Initializers file
module Formtastic #:nodoc:
module Inputs #:nodoc:
class EnumInput < SelectInput #:nodoc:
def to_html
unless options[:collection]
enum = @object.enums(method.to_sym)
choices = enum ? enum.select_options : []
options[:collection] = choices
end
if (value = @object.__send__(method.to_sym))
@bsylvain
bsylvain / trace error
Last active December 11, 2015 17:28
the migration
class AddInvoiceDateToContrat < ActiveRecord::Migration
def change
add_column :contrats, :invoice_date, :date
end
end
@bsylvain
bsylvain / invoice.rb
Last active December 12, 2015 06:38
I'm trying to send a mail with an attached PDF. Here is the code of my mailer, and the start of the noname file attached to it. The noname file has no extension, and cannot be read by acrobat. What i put is what you have when editing the file with a text editor. The PDF works fine when called by a controller. I added the PDF and the show action …
# encoding: utf-8
class Invoice < Prawn::Document
def to_pdf(contrat)
text "BON DE COMMANDE #{contrat.order_number}", :align=>:center, :size=>17
move_down 16
text "Date :",:size=>12
bounding_box([0, 620], :width => 250, :height => 80) do
bounding_box([3, 80], :width => 247, :height => 80) do
move_down 21
text ""
@bsylvain
bsylvain / ERROR
Created February 12, 2013 23:04
In prod only, i have errors when trying to access my backend. The error is about backend.css not precompiled
ActionView::Template::Error (backend.css isn't precompiled):
1: <!DOCTYPE html>
2: <html>
3: <head>
4: <%= stylesheet_link_tag "backend", :media => "all" %>
5: <%= javascript_include_tag "backend" %>
6: <%= csrf_meta_tags %>
7: </head>
app/views/layouts/backend/application.html.erb:4:in `_app_views_layouts_backend_application_html_erb__239998381177855 7849_34715260'
@bsylvain
bsylvain / gist:5358894
Created April 10, 2013 22:08
Nested form that send product id as pricing id to the discount.
<%= semantic_form_for pricing,:url => url_for(:controller =>"backend/discounts",
:action =>"update"),:method=>:put do |p| %>
<%= p.inputs :for => :discounts do |f| %>
<tr>
<td><%=f.input :price,:label=>false%></td>
<td><%=f.input :subscription,:label=>false %></td>
<td><%=f.input :commission_fixed,:label=>false %></td>
<td><%=f.input :commission_percentage,:label=>false %></td>
<td><%=f.input :per_room,:label=>false %></td>
<td>
@bsylvain
bsylvain / form.erb
Last active December 16, 2015 03:39
I try to use nested forms gem on a self joined relation. The add and delete link of the gem (they allow to add and delete a nested element) does not work. I use this gem on another relation in my app and it works fine.
<%= semantic_nested_form_for pricing do |p| %>
<%= p.input :id, :as => :hidden %>
<%= p.inputs :for => :discounts do |f| %>
.... all the fields of the nested element
<%= f.link_to_remove "Supprimer cette remise" %>
<% end %>
<%= p.link_to_add "Ajouter une remise", :discounts %>
<%= p.actions do %>
<%= p.action :submit, :label=>"Créer la remise"%>
@bsylvain
bsylvain / After.rb
Last active December 20, 2015 05:49
Is it the same thing?
factory :user do
email { Faker::Internet.email}
#Lots of declarations
#....
adresse
factory :user_with_recurring_order_to_generate do
after(:build) do |u|
u.contrats << build(:contrat_with_active_product)
end
@bsylvain
bsylvain / gist:6200902
Created August 10, 2013 15:45
Some rake task
require 'fileutils'
desc "Create nondigest versions of all ckeditor digest assets"
task "assets:precompile" do
fingerprint = /\-[0-9a-f]{32}\./
for file in Dir["public/assets/ckeditor/**/*"]
next unless file =~ fingerprint
nondigest = file.sub fingerprint, '.'
FileUtils.cp file, nondigest, verbose: true
end
@bsylvain
bsylvain / rankable.rb
Last active December 21, 2015 02:09
Rankable
require 'active_support/concern'
module Rankable
extend ActiveSupport::Concern
included do
validates :row_order, :presence => true
scope :next_rank, lambda { |rank| where('row_order > ?',rank).order("row_order asc").limit(1)}
scope :previous_rank, lambda { |rank| where('row_order < ?',rank).order("row_order desc").limit(1)}
scope :bigger_rank, order("row_order desc")
before_validation :assign_default_rank
@bsylvain
bsylvain / _form.html.haml
Last active December 21, 2015 16:29
CKeditor does not want to edit
= nested_form_for [ :admin, @gallery ],:html => { :multipart => true } do |f|
.large-12.columns
=f.text_field :title, as: :string
= f.fields_for :gal_images do |gi|
.large-12.columns
= gi.text_field :title, as: :string
= gi.file_field :image
= gi.cktext_area :comment, as: :ckeditor
= gi.text_field :alt, :label => "contenu du alt"
= image_tag gi.object.image.url(:icone)