Programming: base.extend class methods extending including instance methods mixins module mixins modules scope
by bseanvt
leave a comment
Using Module Mixins to Extend Classes and Objects in Ruby
The module and class below demonstrate how to use instance methods and class methods from “module mixins”. The thing to remember is scope. For instance, to use class methods within instance methods you need to call them from the class itself. This is accomplished something like this “self.class.class_method_you_want_to_call”. The example below should make it more clear. It would be natural to assume using “self.method_name” within your module mixin, because after all, instance methods just work when included this way. However, you need to take an extra step when you want to “extend” the behavior of a class.
To extend a class with a mixin you use the method “self.included(base)”. This method takes the parent class “base” as an argument. In the method body you use base.extend to attach class methods. Wrapping up class methods in a module is the easiest way to do it.
module Greeting
# Instance Methods
# Usage Martian.new.hello_world
def hello_world
"Martian says: Hello " + self.class.planet+"!"
end
# Class Methods
# Usage: Martian.planet
def self.included(base)
base.extend ClassMethods
end
module ClassMethods
def planet
"world"
end
end
end
class Martian
include Greeting
def self.location
"Martian lives on "+self.planet
end
end
p Martian.new.hello_world
p Martian.location
Ruby on Rails: builders custom tag builder extending forms label Rails template text field
by bseanvt
1 comment
Extending Rails Form Builders
Extending forms in Rails is simple and will greatly reduce the amount of code in your views. This example is taken right from the Agile Web Development book on Rails(2.1.*) with one minor tweak. I want to pass a label argument along with the field name so that I can display a more human friendly string to represent the form field.
# RAILS_ROOT/app/helpers/custom_tag_builder.rb
class CustomTagBuilder < ActionView::Helpers::FormBuilder
def self.create_tagged_field(method_name)
define_method(method_name) do |label, *args|
label_name = args.first.blank? ? label : args.first[:label] # my change
@template.content_tag("p",
@template.content_tag("label",
label_name.to_s.humanize, :for => "#{@object_name}_#{label}") +" <br/> "+ super)
end
end
field_helpers.each do |name|
create_tagged_field(name)
end
end
You can then use this in your views
form_for @your_model, :builder => CustomTagBuilder do |f| f.text_field :fullname f.text_field :email, :label => "Email (will not be published)"
My change tests for the presence of a label argument otherwise using the name of the form field/model attribute. In this case the fullname attribute will be outputted as “Fullname” while “Email (will not be published)” as the label for the email text field.


