<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: scope code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 05 Sep 2008 16:14:46 GMT</pubDate>
    <description>DZone Snippets: scope code</description>
    <item>
      <title>Making the methods and properties lookups inside a closure be resolved to the delegate.</title>
      <link>http://snippets.dzone.com/posts/show/5922</link>
      <description>// The following code shows how methods and properties will be resolved to the delegate instead of the closure itself.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;// creating a closure&lt;br /&gt;def closure = {&lt;br /&gt;    add "groovy"&lt;br /&gt;    add "java"&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// declaring a list&lt;br /&gt;def list = []&lt;br /&gt;&lt;br /&gt;// making the call methods inside closure be resolved to the list&lt;br /&gt;closure.delegate = list&lt;br /&gt;closure.resolveStrategy = Closure.DELEGATE_FIRST&lt;br /&gt;&lt;br /&gt;// executing the closure&lt;br /&gt;closure();&lt;br /&gt;&lt;br /&gt;// now, the list was changed&lt;br /&gt;list.each { println it }&lt;br /&gt;&lt;br /&gt;// output &lt;br /&gt;// groovy&lt;br /&gt;// java&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 15 Aug 2008 05:00:33 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5922</guid>
      <author>marcospereira (Marcos Silva Pereira)</author>
    </item>
    <item>
      <title>Elegant way of shorten a text string</title>
      <link>http://snippets.dzone.com/posts/show/4578</link>
      <description>this method shortens a plain text string down to complete words contained in given scope (count)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def shorten (string, count = 30)&lt;br /&gt;	if string.length &gt;= count &lt;br /&gt;		shortened = string[0, count]&lt;br /&gt;		splitted = shortened.split(/\s/)&lt;br /&gt;		words = splitted.length&lt;br /&gt;		splitted[0, words-1].join(" ") + ' ...'&lt;br /&gt;	else &lt;br /&gt;		string&lt;br /&gt;	end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Sep 2007 11:47:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4578</guid>
      <author>labuschin (Martin Labuschin)</author>
    </item>
    <item>
      <title>Extending acts_as_taggable to take scope into account</title>
      <link>http://snippets.dzone.com/posts/show/3110</link>
      <description>The acts_as_taggable plugin is great and so useful. But it discards scope, and it doesn't work like any other model attribute in your controller views [using normal @model.update(params[:model]) calls].&lt;br /&gt;&lt;br /&gt;I have changed it to:&lt;br /&gt;* add an alias tag_list= for tag_with (found in the "rails wiki":http://wiki.rubyonrails.com/rails/pages/ActsAsTaggablePluginHowto),&lt;br /&gt;* updated find_tagged_with and count_tagged_with to use scope (after reading an article from Jamis on "ActiveRecord::Base.find":http://weblog.jamisbuck.org/2006/11/20/under-the-hood-activerecord-base-find-part-2)&lt;br /&gt;&lt;br /&gt;Scope is used when doing this for instance:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  Message.with_scope :find =&gt; {:conditions =&gt; ["project_id = ?", @project]} do&lt;br /&gt;    @messages = Message.find_tagged_with(tag.name)&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then only the messages tagged with tag.name that belong to project @project.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;*acts_as_taggable.rb*&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;module ActiveRecord&lt;br /&gt;  module Acts #:nodoc:&lt;br /&gt;    module Taggable #:nodoc:&lt;br /&gt;      def self.included(base)&lt;br /&gt;        base.extend(ClassMethods)  &lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      module ClassMethods&lt;br /&gt;        def acts_as_taggable(options = {})&lt;br /&gt;          write_inheritable_attribute(:acts_as_taggable_options, {&lt;br /&gt;            :taggable_type =&gt; ActiveRecord::Base.send(:class_name_of_active_record_descendant, self).to_s,&lt;br /&gt;            :from =&gt; options[:from]&lt;br /&gt;          })&lt;br /&gt;          &lt;br /&gt;          class_inheritable_reader :acts_as_taggable_options&lt;br /&gt;&lt;br /&gt;          has_many :taggings, :as =&gt; :taggable, :dependent =&gt; true&lt;br /&gt;          has_many :tags, :through =&gt; :taggings&lt;br /&gt;&lt;br /&gt;          include ActiveRecord::Acts::Taggable::InstanceMethods&lt;br /&gt;          extend ActiveRecord::Acts::Taggable::SingletonMethods          &lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      module SingletonMethods&lt;br /&gt;        def find_tagged_with(list)&lt;br /&gt;          tagged_with list&lt;br /&gt;        end&lt;br /&gt;&lt;br /&gt;        def count_tagged_with(list)&lt;br /&gt;          tagged_with list, :count&lt;br /&gt;        end&lt;br /&gt;&lt;br /&gt;        # DRY sql query and handle scope&lt;br /&gt;        protected&lt;br /&gt;        def tagged_with(list, type = :find)&lt;br /&gt;          if type == :count&lt;br /&gt;            sql = "SELECT COUNT(DISTINCT #{table_name}.#{primary_key}) AS cnt "&lt;br /&gt;          else&lt;br /&gt;            sql = "SELECT DISTINCT #{table_name}.* "&lt;br /&gt;          end&lt;br /&gt;          sql &lt;&lt; "FROM #{table_name}, tags, taggings "&lt;br /&gt;&lt;br /&gt;          conditions = [&lt;br /&gt;            "#{table_name}.#{primary_key} = taggings.taggable_id " +&lt;br /&gt;            "AND taggings.taggable_type = ? " +&lt;br /&gt;            "AND taggings.tag_id = tags.id AND tags.name IN (?) ",&lt;br /&gt;            acts_as_taggable_options[:taggable_type], list&lt;br /&gt;            ]&lt;br /&gt;          add_conditions!(sql, conditions)&lt;br /&gt;&lt;br /&gt;          result = find_by_sql(sql)&lt;br /&gt;          (type == :count) ? result.first.cnt.to_i : result&lt;br /&gt;        end&lt;br /&gt;&lt;br /&gt;      end&lt;br /&gt;      &lt;br /&gt;      module InstanceMethods&lt;br /&gt;        def tag_with(list)&lt;br /&gt;          Tag.transaction do&lt;br /&gt;            taggings.destroy_all&lt;br /&gt;&lt;br /&gt;            Tag.parse(list).each do |name|&lt;br /&gt;              if acts_as_taggable_options[:from]&lt;br /&gt;                send(acts_as_taggable_options[:from]).tags.find_or_create_by_name(name).on(self)&lt;br /&gt;              else&lt;br /&gt;                Tag.find_or_create_by_name(name).on(self)&lt;br /&gt;              end&lt;br /&gt;            end&lt;br /&gt;          end&lt;br /&gt;        end&lt;br /&gt;&lt;br /&gt;        # allow using active record update_attributes() and others by using tag_list to set&lt;br /&gt;        # and read the tag list&lt;br /&gt;        alias tag_list= tag_with&lt;br /&gt;&lt;br /&gt;        def tag_list&lt;br /&gt;          tags.collect { |tag| tag.name.include?(" ") ? "'#{tag.name}'" : tag.name }.join(" ")&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 08 Dec 2006 21:49:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3110</guid>
      <author>zlaj (mathieu l)</author>
    </item>
  </channel>
</rss>
