Appartamento in Affitto Milano – Washington

Plugins included in bundle

  • paperclip
  • authenticated_system
  • better_error_messages
  • better_error
  • better_nested_set
  • better_nested_set_helper
  • better_tag_helper
  • pdf_helper
  • prince
  • without_table
  • serializo

Paperclip

Paperclip is a super simply and powerful plugin for manage uploads.

The author say:

For some reason, file attachment is annoying. I don’t know why, and I know a lot of people have attempted to solve the problem in the past, myself included. Yet it still is. Having gotten fed up with gotchas and design decisions that we didn’t agree with, I went and wrote Paperclip on the plane to RailsConf last year. We’ve been using it here in various forms since and IMHO it’s the way to handle uploads, and finally decided that it should be released.

Let’s know how use it:

1 class User < ActiveRecord::Base
2   has_attached_file :avatar,
3                     :styles => { :square => ["64x64#", :png],
4                                  :small  => "150x150>" }
5 end

Styles are an hash of thumbnail styles and their geometries. You can find more about geometry strings at the ImageMagick website (www.imagemagick.org/script/command-line-options.php#resize). Paperclip also adds the ”#” option (e.g. “50×50#”), which will resize the image to fit maximally inside the dimensions and then crop the rest off (weighted at the center). The default value is to generate no thumbnails.

For make an avatar for user we can do:

script/generate attachment user avatar

 1 class AddAvatarToUser < ActiveRecord::Migration
 2   def self.up
 3     add_column :users, :avatar_file_name, :string
 4     add_column :users, :avatar_content_type, :string
 5     add_column :users, :avatar_file_size, :integer
 6   end
 7 
 8   def self.down; ...; end
 9 end

Now with lipsiadmin we can simply do for generate our views:
script/generate lipsiadmin_page user -i avatar

Remember that -i can accept an array of image like: lipsiadmin_page -i image1,image2,image3
We also make a view for file like pdf so we have also: lipsiadmin_page -l file1,file2

For more details see revision r35

The view are like:

1 <% form_for :user, :html => { :multipart => true } do |form| %>
2   <%= form.file_field :avatar %>
3 <% end %>

And for show it we can do:

1 <%= image_tag @user.avatar.url %>
2 <%= image_tag @user.avatar.url(:medium) %>
3 <%= image_tag @user.avatar.url(:thumb) %>

Super simple yea?

Authenticated System

For manage accounts of this admin we use a library based on the beautifull plugin of Rick Olson

We can manage Admin User and Site User.

Better Error Messages

I written a new <= error_messages_for :account %> now called <= ext_error_messages_for :account %> for show in a better way errors.

Better Error

I included in ActiveRecord a new method for show errors in new Ext Popup

1 module LipsiaSoft
2   module BetterErrors
3     def show_errors
4       return "- " + self.errors.full_messages.join("<br />- ")
5     end
6   end
7 end

Better Nested Set and Helper

This library is for manage Ext tree (see howto) in this way:

 1   def get_tree(categories, parent)  
 2     data = Array.new  
 3     categories.each { |category|  
 4       if !category.leaf?  
 5         if data.empty?  
 6           data =   [{"text"  =>  category.name, "id"  => category.id, "leaf"  => false,  
 7                      "children" => get_tree(category.children,category) }]   
 8         else  
 9           data.concat([{"text"  =>  category.name, "id"  => category.id, "leaf"  => false,  
10                          "children" => get_tree(category.children,category)}])  
11         end  
12       else  
13         data.concat([{"text" => category.name, "id" => category.id, 
14                                 "cls" => "folder", "leaf" => false, "children" => []}])   
15       end  
16     }  
17     return data  
18   end

Better Tag Helper

I added to textfield passwordfield textare submit tags new default attributes and style, so you can simply add borders, background and others, but also for textfield a new attribute:

1 def text_field_tag(name, value = nil, options = {})
2   options[:class] ||= "text_field" 
3   if options[:onclick] == :clear_value
4     options.delete(:onclick)
5     options.merge!(:onblur => "if(this.value=='')this.value=this.defaultValue;",

So you can add some default text to your textfield:

1 <%= text_field :subscription, :email, :value => "Enter your e-mail", :onclick => :clear_value %>

Pdf Helper and Prince

I manage much pdf and I’ve no time for build them… so I convert my html in pdf with Prince Xml for example you can make a pdf with few lines with dry.

1 def print
2   @recipe = Recipe.find(params[:id])
3   if logged_in?
4     make_and_send_pdf("/site/recipes/print", "Italyabroad_Recipe_#{@recipe.id}.pdf")
5   else
6     redirect_to :controller => :base, :action => :login
7   end
8 end

ActiveRecord WithoutTable

Based on the beautifull plugin Jonathan Viney it’s usefull for manage data tableless like contact form and output errors/warnings

Serializo

Serializo is a very simple library for convert attributes of an serialization in method so we can use it in our form withou problem.

Example of use:

 1 require 'digest/sha1'
 2 class Account < ActiveRecord::Base
 3   # Virtual attribute for the unencrypted password
 4   attr_accessor :password, :data, :active
 5 
 6   serialize :permissions
 7 
 8   ...
 9 
10   def permissions
11     Serializo.generate(read_attribute(:permissions))
12   end
13 
14   def permissions=(perm)
15     write_attribute(:permissions, perm.to_hash)
16   end

So in the view we can do:
myaccount.permission.can_read = true

This is very usefull in form like:

1 <% fields_for "account[permissions]", @account.permissions do |perm| %>
2   <div><%= perm.text_field :dynamic_field, :style => "width:100%" %></div>
3 <% end %>

Also available in: HTML TXT