c# barcode generator wpf Adding acts_as_taggable, in app/models/event.rb in Font

Make QR Code JIS X 0510 in Font Adding acts_as_taggable, in app/models/event.rb

Listing 10-2. Adding acts_as_taggable, in app/models/event.rb
QR-Code Printer In None
Using Barcode creation for Font Control to generate, create QR-Code image in Font applications.
www.OnBarcode.com
GTIN - 128 Creator In None
Using Barcode creation for Font Control to generate, create EAN 128 image in Font applications.
www.OnBarcode.com
class Event < ActiveRecord::Base acts_as_taggable #... end
Code 39 Extended Creation In None
Using Barcode drawer for Font Control to generate, create Code-39 image in Font applications.
www.OnBarcode.com
Paint Barcode In None
Using Barcode creator for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
CHAPTER 10 EXTENDING RAILS WITH PLUGINS
QR Code Printer In None
Using Barcode generator for Font Control to generate, create QR Code JIS X 0510 image in Font applications.
www.OnBarcode.com
Create PDF417 In None
Using Barcode drawer for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
Just like other class-level model declarations, such as has_many and belongs_to, acts_as_taggable results in the addition of several methods to the class in which it s used. Two instance methods are supplied for adding tags to and listing the tags of a particular object: tag_with(list) and tag_list. Additionally, a class-level finder is added to enable searching of items by a list of tags: find_tagged_with. Let s take this for a quick spin on the console so you can see how it works.
DataMatrix Encoder In None
Using Barcode maker for Font Control to generate, create DataMatrix image in Font applications.
www.OnBarcode.com
Create 2 Of 5 Interleaved In None
Using Barcode creator for Font Control to generate, create 2 of 5 Interleaved image in Font applications.
www.OnBarcode.com
$ ./script/console Loading development environment. >> e = Event.find(:first) => #<Event:0x3068e04> >> e.tag_with "interesting parties activism" => ["interesting", "parties", "activism"] >> Event.find_tagged_with "activism" => [#<Event:0x30283cc>] >> e.tag_list => "interesting parties activism" >> e.tags => [#<Tag:0x3111a54, #<Tag:0x311198c>, #<Tag:0x3111964>]
QR Code 2d Barcode Creator In .NET Framework
Using Barcode generation for ASP.NET Control to generate, create Denso QR Bar Code image in ASP.NET applications.
www.OnBarcode.com
QR Code 2d Barcode Generation In None
Using Barcode encoder for Word Control to generate, create QR Code image in Microsoft Word applications.
www.OnBarcode.com
To implement the tagging feature, we need to be able to assign tags to existing and new events. This is a simple matter of adding a tags field to our event form partial and handling the tags parameter in the controller. Modify the partial in app/views/events/ _form.rhtml to include the tags field at the very bottom of the form, as shown in Listing 10-3.
Data Matrix Drawer In None
Using Barcode generation for Software Control to generate, create DataMatrix image in Software applications.
www.OnBarcode.com
Drawing Code 128 Code Set B In None
Using Barcode creator for Online Control to generate, create Code 128B image in Online applications.
www.OnBarcode.com
Listing 10-3. Adding a Tags Field to the Form, in app/views/events/_form.rhtml
Barcode Encoder In Java
Using Barcode generator for Java Control to generate, create Barcode image in Java applications.
www.OnBarcode.com
Code-39 Maker In None
Using Barcode maker for Word Control to generate, create Code 39 Extended image in Office Word applications.
www.OnBarcode.com
<p><label for="event_tags">Tags <span>(space separated)</span></label><br/> <%= text_field_tag 'tags', @event.tag_list, :size => 40 %></p>
Draw Barcode In None
Using Barcode creation for Office Excel Control to generate, create Barcode image in Office Excel applications.
www.OnBarcode.com
Barcode Encoder In .NET
Using Barcode printer for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
We use the text_field_tag helper to create a field for tags. The second argument to text_field_tag is the field s initial value, which we set using the tag_list method, which returns a string of space-separated tags. Figure 10-1 shows the result when rendered in a browser.
Drawing PDF-417 2d Barcode In None
Using Barcode drawer for Excel Control to generate, create PDF 417 image in Excel applications.
www.OnBarcode.com
DataMatrix Decoder In Visual Basic .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
CHAPTER 10 EXTENDING RAILS WITH PLUGINS
Generate Data Matrix ECC200 In Objective-C
Using Barcode generation for iPhone Control to generate, create Data Matrix image in iPhone applications.
www.OnBarcode.com
Barcode Scanner In Java
Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Figure 10-1. Editing an event with tags
All that s left to do is handle the tags field in the events controller (app/controller/ events_controller.rb). We ll need to modify both the create and update actions to invoke the tag_with method, passing in params[:tags] (if it exists). Here s how it looks:
@event.tag_with(params[:tags]) if params[:tags]
The updated create and update actions are shown in Listing 10-4. The new code is listed in bold.
Listing 10-4. Modified Create and Update Actions, in app/controller/events_controller.rb
class EventsController < ApplicationController #... def create
CHAPTER 10 EXTENDING RAILS WITH PLUGINS
@event = current_user.events.build(params[:event]) @event.categories << Category.find(params[:categories]) unless params[:categories].blank @event.tag_with(params[:tags]) if params[:tags] if @event.save flash[:notice] = 'Event was successfully created.' redirect_to :action => 'index' else render :action => 'new' end end #... def update @event = current_user.events.find(params[:id]) @event.attributes = params[:event] @event.category_ids = params[:categories] @event.tag_with(params[:tags]) if params[:tags] if @event.save flash[:notice] = 'Event was successfully updated.' redirect_to :action => 'show', :id => @event else render :action => 'edit' end end #... end
That s all there is to it! Just a few minutes after installing the acts_as_taggable plugin, we are finished implementing tagging in our application. As you can see, plugins can definitely boost productivity.
Note The acts_as_taggable plugin was originally created to demonstrate polymorphic interfaces in Active Record. We think it works great for simple cases, and it may be all you ever need. If you want something more robust, try one of the various community-supported plugins, which you can find by searching: http://agilewebdevelopment.com/plugins/search search=acts_as_taggable.
CHAPTER 10 EXTENDING RAILS WITH PLUGINS
Creating Your Own Plugin
Whenever you find yourself repeatedly writing the same code snippets in different Rails applications, you have a very strong case for extracting this functionality in your own plugin. This will help you keep the business logic for this functionality in one location, instead of in multiple applications, and make it possible to fix bugs in a single place. To demonstrate how to create your own plugin, we ll make one to implement search functionality for a given model, so that the search code can be reused easily. We ll call this plugin simply_searchable. Rails comes with a plugin generator that you can use to create the skeleton for a new plugin. You just need to tell it the name of the plugin you want to create:
$ ./script/generate plugin simply_searchable
create create create create create create create create create create create create
vendor/plugins/simply_searchable/lib vendor/plugins/simply_searchable/tasks vendor/plugins/simply_searchable/test vendor/plugins/simply_searchable/README vendor/plugins/simply_searchable/MIT-LICENSE vendor/plugins/simply_searchable/Rakefile vendor/plugins/simply_searchable/init.rb vendor/plugins/simply_searchable/install.rb vendor/plugins/simply_searchable/uninstall.rb vendor/plugins/simply_searchable/lib/simply_searchable.rb vendor/plugins/simply_searchable/tasks/simply_searchable_tasks.rake vendor/plugins/simply_searchable/test/simply_searchable_test.rb
The generator created quite a few files. As you can see from the generator s output, they re all in the vendor/plugins/#{plugin_name} directory. The structure for a plugin is quite specific and follows normal Ruby library conventions. Table 10-2 describes the function of each file and directory.
Copyright © OnBarcode.com . All rights reserved.