Include helpers in controllers
Posted In: rails, ruby. By FaziBear

Yes, i know!
Helpers are modules that provide methods which are automatically usable in your view.
But somtimes you want to use it in controllers ;)
This is my solution. Helpers are available in module named 'Helpers', truth about enzyte so they don't brake anything. All rails helpers and named routes are included by default.
This is simple usage:
view plainprint?
class ExampleController < ApplicationController
include_helper ApplicationHelper, PicturesHelper

def show
...
flash[:notice] = Helpers::flash_with_picture('Hello from helper')
...
end
end
And method to add to ApplicationController class.
view plainprint?
class ApplicationController < ActionController::Base
def self.include_helper(*args)
require 'action_controller/integration'
class_eval do
helpers = const_defined?('Helpers') ? const_get('Helpers') : Module.new do
@@controller = ActionController::Integration::Session.new
def self.method_missing(method, *args, &block)
if @@controller && method.to_s =~ /_path$|_url$/
@@controller.send(method, *args, &block)
else
raise NoMethodError, "undefined method `#{method}' for #{self}"
end
end
end
ActionView::Helpers.constants.each do |constant|
helpers.extend ActionView::Helpers.const_get(constant) if ActionView::Helpers.const_get(constant).instance_of?(Module)
end
if args.instance_of?(Array)
args.each do |helper|
helpers.extend helper if helper.instance_of?(Module)
end
elsif args.instance_of?(Module)
helpers.extend args
end
const_set( 'Helpers', helpers ) unless const_defined?('Helpers')
end
end
end

Up?
Comments: 1
April
29
GEdit With Multi Terminal
Posted In: gedit, python. By FaziBear

GEdit is nice developer editor for GNOME. Textmate like snippets, file browser and embedded terminal. Last one is a nice feature, but default plugin gives you only one terminal. But wait, plugins are written in python. This is my third attempt to modify python code... It was not so bad, after few print(), dir(), and id() I can add and remove terminal windows within gedit :)

How to install ? Copy all files to ~/.gnome2/gedit/plugins, disable terminal plugin, restart gedit and enable mterminal plugin.

How to use it ? To add new terminal right-click on terminal window and choose 'New Terminal'. If you want to remove it, right-click and choose 'Remove Terminal'.

Plugin/Sources are located here.

I hope you like it.
Up?
Comments: 7
March
14
UWA Google Suggest
Posted In: javascript, netvibes, uwa. By FaziBear

Google Suggest is now UWA style widget. Check this out.
Up?
Comments: 2
March
7
Id of nil object
Posted In: ruby. By FaziBear

view plainprint?
nil.id == 4 # => true
Sweet truth ;)

Up?
Comments: 0
February
29
Generating Methods in Ruby
Posted In: ruby, tips. By FaziBear

I was trying to find a way for generating dynamic methods.
My first attempt was very ugly and use a simple eval:
view plainprint?
class DynamicMethods
%w{ one two three }.each do |z|
eval "def #{z}(param) puts 'Hello from \\'#{z}\\' method with \\''+param+'\\' param' end"
end
end

Hmm... no... all this escape characters ... there must be a better way. And there is. Using %Q{} ...
view plainprint?
class DynamicMethods
%w{ one two three }.each do |z|
eval %Q{
def #{z}(param)
puts "Hello form '#{z}' method with '\#{param}' param"
end
}
end
end

That's better ... more readable, but i don't have syntax highligt for this whole string. Lets try define_method ...
view plainprint?
class DynamicMethods
%w{ one two three }.each do |z|
define_method z do |param|
puts "Hello from '#{z}' method with '#{param}' param"
end
end
end

There it is. No eval, no big strings, just a method name and code block. Beautful. All examples give you same output.
view plainprint?
DynamicMethods.new.one("hello") # => Hello from 'one' method with 'hello' param

Up?