<?xml version="1.0" encoding="UTF-8"?><feed
  xmlns="http://www.w3.org/2005/Atom"
  xmlns:thr="http://purl.org/syndication/thread/1.0"
  xml:lang="en"
  xml:base="http://www.binarylogic.com/wp-atom.php"
   >
	<title type="text">Binary Logic</title>
	<subtitle type="text">Ben Johnson's thoughts and programming techniques</subtitle>

	<updated>2010-01-23T21:19:58Z</updated>
	<generator uri="http://wordpress.org/" version="2.8">WordPress</generator>

	<link rel="alternate" type="text/html" href="http://www.binarylogic.com" />
	<id>http://feeds.binarylogic.com/binarylogic</id>
	<link rel="self" type="application/atom+xml" href="http://www.binarylogic.com/feed/atom/" />

			<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Polymorphic association support added to searchlogic]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2010/01/23/polymorphic-association-support-added-to-searchlogic/" />
		<id>http://www.binarylogic.com/?p=839</id>
		<updated>2010-01-23T21:19:58Z</updated>
		<published>2010-01-23T21:19:58Z</published>
		<category scheme="http://www.binarylogic.com" term="Searchlogic" /><category scheme="http://www.binarylogic.com" term="polymorphic" />		<summary type="html"><![CDATA[If you have ever worked with polymorphic associations you know that using them in a search can be a pain in the ass. Rightfully so, because a polymorphic association is ambiguous, in order for ActiveRecord to do its magic with creating joins it needs more information that it can&#8217;t get. Such as, are you wanting [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2010/01/23/polymorphic-association-support-added-to-searchlogic/"><![CDATA[<p>If you have ever worked with polymorphic associations you know that using them in a search can be a pain in the ass. Rightfully so, because a polymorphic association is ambiguous, in order for ActiveRecord to do its magic with creating joins it needs more information that it can&#8217;t get. Such as, are you wanting to search type A or type B? In my projects I&#8217;ve been dealing with this by writing my own named scopes with my own joins. Not a big deal. But here&#8217;s the problem&#8230;.</p>
<p>My favorite feature in Searchlogic is named scope delegation. I&#8217;ve noticed in a number of projects I&#8217;ve seen where programmers copy over named scope logic into related models. Which makes sense, because AR gives you no way of easily accessing related scopes. For example:</p>
<pre>
Order.named_scope :pending, :conditions => {:state => "pending"}
User.named_scope :pending_orders, :joins => :orders, :conditions => "orders.state = 'pending'"
</pre>
<p>Most people would look at that and never see anything wrong with it. What is the conditions for a pending order changes?</p>
<pre>
Order.named_scope :pending, :conditions => "orders.state = 'pending' AND approved = true
</pre>
<p>Now you have to remember to change this logic in associated models. This is a programming no-no. With searchlogic you don&#8217;t even need to touch the User model. You can access the order&#8217;s named scope, and it delegates that call:</p>
<pre>
User.orders_pending
</pre>
<p>Now when you change what defines an order as pending it will change everywhere in your application. This is great, but where this feature comes to a screeching halt is when you encounter a polymorphic association. This is because AR has no internal support for this either. For example, you can&#8217;t do:</p>
<pre>
Audit.belongs_to :auditable, :polymorphic => true
User.has_many :audits, :as => :auditable

Audit.all(:joins => :user)
</pre>
<p>Obviously the above won&#8217;t work. AR has no idea that there is a relationship with users through the polymorphic auditable relationship. To overcome this you have to write your own join. Again, there is nothing wrong with writing SQL, I don&#8217;t want to send the wrong message. The problem is that you are back to square one with my named scope delegation problem above. To solve this I added a way to overcome this. With searchlogic you could do this:</p>
<pre>
Audit.auditable_user_type_name_like("ben")
</pre>
<p>Searchlogic will take care of creating the join on the polymorphic relationship and then delegate the call to the User.name_like named_scope. I think it&#8217;s pretty nifty.</p>
<p>I hope I explained this well. I figured I would mention the new feature because I&#8217;m sure I&#8217;m not the only person that has encountered this problem.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Polymorphic%20association%20support%20added%20to%20searchlogic&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2010%2F01%2F23%2Fpolymorphic-association-support-added-to-searchlogic%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2010/01/23/polymorphic-association-support-added-to-searchlogic/#comments" thr:count="6"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2010/01/23/polymorphic-association-support-added-to-searchlogic/feed/atom/" thr:count="6"/>
		<thr:total>6</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Using Geokit with Searchlogic]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2010/01/09/using-geokit-with-searchlogic/" />
		<id>http://www.binarylogic.com/?p=832</id>
		<updated>2010-01-11T08:30:34Z</updated>
		<published>2010-01-10T03:06:21Z</published>
		<category scheme="http://www.binarylogic.com" term="Searchlogic" /><category scheme="http://www.binarylogic.com" term="geokit" />		<summary type="html"><![CDATA[For those of you that don&#8217;t know, geokit is a great little gem that allows you to geocode addresses. It adds in very easy to use options in the ActiveRecord find method. Ex:

# will return all venues within 10 miles of the 10018 zip code
Venue.find(:all, :origin => "10018", :within => 10)

The problem is that you [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2010/01/09/using-geokit-with-searchlogic/"><![CDATA[<p>For those of you that don&#8217;t know, <a href="http://github.com/andre/geokit-gem">geokit</a> is a great little gem that allows you to geocode addresses. It adds in very easy to use options in the ActiveRecord find method. Ex:</p>
<pre>
# will return all venues within 10 miles of the 10018 zip code
Venue.find(:all, :origin => "10018", :within => 10)
</pre>
<p>The problem is that you can&#8217;t use geokit options in a named scope. Since searchlogic is named scope driven this presented a problem. So I wrote a little module that transfers geokit options from named scopes to the actual arguments of the method:</p>
<pre class="ruby">
module GeokitForNamedScopes
  OPTIONS = [:origin, :within, :beyond, :range, :formula, :bounds]

  def find(*args)
    super(*transfer_from_scope_to_args(args))
  end

  def count(*args)
    super(*transfer_from_scope_to_args(args))
  end

  private
    def transfer_from_scope_to_args(args)
      find_options = scope(:find)
      if find_options.is_a?(Hash)
        options = args.extract_options!
        OPTIONS.each do |key|
          options[key] = find_options.delete(key) if find_options.key?(key)
        end
        args << options
      else
        args
      end
    end
end

class ActiveRecord::Base
  class << self
    def acts_as_mappable(*args)
      result = super(*args)
      extend GeokitForNamedScopes

      GeokitForNamedScopes::OPTIONS.each do |key|
        named_scope key, lambda { |value| {key => value} }
      end

      result
    end

    VALID_FIND_OPTIONS += GeokitForNamedScopes::OPTIONS
  end
end
</pre>
<p>It&#8217;s far from perfect. The glaring problem is obviously redefining the VALID_FIND_OPTIONS constant, which throws a warning. But it works and it&#8217;s good enough for now. If anyone has any suggestions on how to improve this I&#8217;d to hear them. But you should be able to use geokit options in a named_scope with the above code. Ex:</p>
<pre>
Venue.origin("10017").within(10).all # => will return all venues within 10 miles of 10017
</pre>
<h2>Using this through associations</h2>
<p>Geokit doesn&#8217;t really support making these calls through associations. But if you read the <a href="http://github.com/andre/geokit-rails">documentation</a>. You will notice they do support this:</p>
<pre>
acts_as_mappable :through => :whatever
</pre>
<p>You can use that feature to bridge the gap</p>
<p>Hopefully this helps some of you.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Using%20Geokit%20with%20Searchlogic&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2010%2F01%2F09%2Fusing-geokit-with-searchlogic%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2010/01/09/using-geokit-with-searchlogic/#comments" thr:count="4"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2010/01/09/using-geokit-with-searchlogic/feed/atom/" thr:count="4"/>
		<thr:total>4</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Great marketing or a great product?]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/12/07/great-marketing-or-a-great-product/" />
		<id>http://www.binarylogic.com/?p=823</id>
		<updated>2009-12-08T08:41:13Z</updated>
		<published>2009-12-08T03:41:25Z</published>
		<category scheme="http://www.binarylogic.com" term="Uncategorized" /><category scheme="http://www.binarylogic.com" term="marketing business" />		<summary type="html"><![CDATA[I know this post is kind of out of the norm, but what the hell, I figured I would let it fly. Today I was thinking a lot about business and what makes a company successful. I was thinking specifically about marketing and I came up with an interesting scenario:
Say you had money to invest [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/12/07/great-marketing-or-a-great-product/"><![CDATA[<p>I know this post is kind of out of the norm, but what the hell, I figured I would let it fly. Today I was thinking a lot about business and what makes a company successful. I was thinking specifically about marketing and I came up with an interesting scenario:</p>
<p>Say you had money to invest and you had to pick one of these companies to invest in. Both companies are in new industries that have pretty good potential for growth. Which one would you pick?</p>
<ol>
<li>A company that clearly has the best product, but has an average or below average marketing strategy.</li>
<li>A company with an average or below average product, but has an outstanding marketing strategy.</li>
</ol>
<p>The above facts are set in stone. You can&#8217;t say you would change their marketing strategy or improve their product. You had to pick one of the companies given the current facts above, with no assumptions as to what can or might happen in the future.</p>
<p>I am also aware that if you were actually investing into a company you would require much more information, and there are a number of factors that would sway your decision. But setting that all aside, and you had to pick one right now given the above information, which one would you pick?</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Great%20marketing%20or%20a%20great%20product%3F&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F12%2F07%2Fgreat-marketing-or-a-great-product%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/12/07/great-marketing-or-a-great-product/#comments" thr:count="9"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/12/07/great-marketing-or-a-great-product/feed/atom/" thr:count="9"/>
		<thr:total>9</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Authlogic: keeping you in the loop]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/11/21/authlogic-keeping-you-in-the-loop/" />
		<id>http://www.binarylogic.com/?p=820</id>
		<updated>2009-11-21T22:17:43Z</updated>
		<published>2009-11-21T22:17:43Z</published>
		<category scheme="http://www.binarylogic.com" term="Authlogic" /><category scheme="http://www.binarylogic.com" term="authentication" /><category scheme="http://www.binarylogic.com" term="v3" />		<summary type="html"><![CDATA[I figured I would post an update keeping everyone in the loop. I created a new branch for authlogic locally and I&#8217;ve been making some changes mentioned in this post. Some of the changes I&#8217;m making are still somewhat experimental and I&#8217;m playing around with the code to see if I like the end result. [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/11/21/authlogic-keeping-you-in-the-loop/"><![CDATA[<p>I figured I would post an update keeping everyone in the loop. I created a new branch for authlogic locally and I&#8217;ve been making some changes mentioned <a href="http://www.binarylogic.com/2009/08/13/authlogic-ideas/">in this post</a>. Some of the changes I&#8217;m making are still somewhat experimental and I&#8217;m playing around with the code to see if I like the end result. Figuring out a way to make this ORM agnostic has proven difficult, simply because there is no standard. They all follow a similar pattern, but the method naming conventions and some of the feature implementations are vastly different. I&#8217;m also not sure you could even set a standard. Regardless, I do think this is an important feature since there is a lot of movement around alternative storage solutions such as MongoDB, CouchDB, etc.</p>
<p>Splitting out some of the authentication code into &#8220;authenticators&#8221; has worked out great so far. More importantly, its more or less a blueprint for people to extend Authlogic and provide alternate authentication solutions (openid, facebook connect, oauth, etc.).</p>
<p>The last major change I&#8217;ve been making is removing validations in the acts_as_authentic module. Validations add some clutter to authlogic, some might say its a necessary clutter, but I&#8217;m leaning towards leaving it out. Validation is such an easy thing to do. ActiveRecord provides very simple methods for doing this, I also feel like its gets in the way for certain edge cases. Authlogic will still provide some nice methods to make your life easier, like validating the format of an email address, the main difference being that you have to explicitly implement this validation. No validations will be automatically added. Lastly, this makes ORM abstraction a little easier.</p>
<p>So that&#8217;s that. I&#8217;ll keep you updated when I start to push out some code and what not. I&#8217;m probably going to label this release as v3, because of the validation changes and some of the other major changes that might break backwards compatibility.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Authlogic%3A%20keeping%20you%20in%20the%20loop&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F11%2F21%2Fauthlogic-keeping-you-in-the-loop%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/11/21/authlogic-keeping-you-in-the-loop/#comments" thr:count="5"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/11/21/authlogic-keeping-you-in-the-loop/feed/atom/" thr:count="5"/>
		<thr:total>5</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Discontinuing Resourcelogic]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/10/06/discontinuing-resourcelogic/" />
		<id>http://www.binarylogic.com/?p=805</id>
		<updated>2009-10-06T04:33:08Z</updated>
		<published>2009-10-06T04:31:47Z</published>
		<category scheme="http://www.binarylogic.com" term="Resourcelogic" /><category scheme="http://www.binarylogic.com" term="discontinuing" /><category scheme="http://www.binarylogic.com" term="resources" />		<summary type="html"><![CDATA[Lately I&#8217;ve been really going back and forth between using tools like resourcelogic, inherited_resources, resource_controller, etc. And I gave each one of these libraries a more than fair shot. I&#8217;ve used all of them extensively, that&#8217;s the whole reason I created resourcelogic to begin with. I really liked resource_controller, so much that I decided to [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/10/06/discontinuing-resourcelogic/"><![CDATA[<p>Lately I&#8217;ve been really going back and forth between using tools like <a href="http://github.com/binarylogic/resourcelogic">resourcelogic</a>, <a href="http://github.com/josevalim/inherited_resources">inherited_resources</a>, <a href="http://github.com/giraffesoft/resource_controller">resource_controller</a>, etc. And I gave each one of these libraries a more than fair shot. I&#8217;ve used all of them extensively, that&#8217;s the whole reason I created resourcelogic to begin with. I really liked resource_controller, so much that I decided to take it and kind of make my own version called resourcelogic. I ended up changing it quite a bit, and it worked out well, but it got out of hand. So I&#8217;m going to address why I advise against using libraries like this:<br />
<span id="more-805"></span></p>
<h2>Deviating from standards makes it harder to work with other programmers</h2>
<p>You have to think that certain things in rails are there for a reason. Rails is a result of thousands of programmer&#8217;s ideas, its used by extensively by programming teams large and small, and has tremendous success. There is a reason for that. Chances are, you aren&#8217;t going to sit around one day and come up with this idea that takes rails to the next level. I&#8217;m not trying to be a Debbie Downer and tell people not to try and improve rails or be innovative. That&#8217;s not the message I&#8217;m trying to send here. What I&#8217;m trying to say is that if you are sitting around and you come up with this crazy idea that deviates quite a bit from the rails standards, it&#8217;s probably wrong, even if you think it&#8217;s cool. I thought I did that with resourcelogic. I thought I was Mr. Awesome and I came up with this new development style for rails that was better than anything done before. I know that sounds pretty conceited but I was on my high horse. I thought the development style behind resourcelogic was awesome and I was going to share it with the rails community and people were going to eat it up.</p>
<p>So I ran with it, I developed an entire project with it by myself. Probably the most important project I&#8217;ve ever developed. I gave all of these libraries a more than fair shot. In the end, it made my project more confusing. The red flag was showing my code to another programmer. I had to explain what was going on and he was still kind of confused. Once he got it, he didn&#8217;t like it. This is not good. I didn&#8217;t like the thought of having to go through this with every programmer I was going to hire for this project. Which leads me to my next point:</p>
<h2>You lose intent</h2>
<p>This is probably my biggest complaint: you lose intent. There is a lot of code hidden away, so when you see small controllers with 4 &#8211; 5 lines of code providing all of this application specific functionality it kind of makes you wonder what is going on. There is no direct intent saying &#8220;this is what I&#8217;m doing&#8221;. There&#8217;s a lot of magic. My controllers looked like this:</p>
<pre class="ruby">
create.before { ... }
create.wants.xml { ... }
create.wants.js { ... }
create.after { ... }
index.before { ... }

def my_own_custom_action
...
end
</pre>
<p>It just felt wrong. There is something refreshing about seeing this:</p>
<pre class="ruby">
def create
  @user = User.new(params[:user])
  respond_to do |wants|
    if @user.save
      wants.html { redirect_to users_url }
      wants.js { render :inline => "window.location = '<%= users_url %>';" }
   else
     wants.html { render :action => :new }
     wants.js { ... }
   end
  end
end
</pre>
<p>Is that more code? Yes. Who said more code is bad when it makes more sense? I can look at the create method and know EXACTLY what is going on. I can add in some weird edge case if I need. I can do whatever the hell I want and I&#8217;m not confined to the standards set in resourcelogic. I don&#8217;t feel like I&#8217;m deviating from a pattern by doing that either. Sure, with resourcelogic you can write your own create method, but then I feel dirty, like I&#8217;m not following the pattern my application has defined.</p>
<h2>Upgrading rails</h2>
<p>What example would you feel more comfortable with if you were upgrading from rails 2 to 3? The one that uses a pattern that deviates from the rails standards, or one that uses the most basic of rails code? I would feel better with the second. That is standard rails code that I know for a 100% fact is going to be supported in Rails 3. Sure, you could up date the library in the first example to work with rails 3, but in my opinion thats a sign the library is messing with code it shouldn&#8217;t.</p>
<h2>Conclusion</h2>
<p>I&#8217;m not trying to knock libraries like resource_controller or inherited_resources, hell I wrote my own version called resourcelogic. So I would be knocking myself if I did. I just don&#8217;t think they are needed. Rails is simple to begin with. There is nothing wrong with writing out a full controller method. It&#8217;s simple, its direct, its clear, and it follows the rails standards. I can pick any programmer from the rails community and not have to say a word and they would understand a standard rails action. While at the same time I could show them a &#8220;resource controller&#8221; and a lot of people would wonder what the hell is going on. More importantly, edge cases feel clean. If a controller needs to do something a little bit different, that&#8217;s fine. I have full control of what is going on. I don&#8217;t have to use some weird API with before and after hooks to add in edge cases.</p>
<p>So the bottom line is that I am stopping support for resourcelogic. I will more than likely remove it from github because I don&#8217;t want to send people down the wrong path thinking its a good tool to use. For a simple project its probably fine, but in the end it&#8217;s just not worth it. All of the time and effort I spent writing resourcelogic and implementing it into my app was kind of a waste. With that time I could have made some major advancements and improvements to my application. But it&#8217;s things like this that make you a better programmer, you live and you learn. The lesson I learned is to not deviate from the rails standards too much. Use what they give you, chances are it will work out just fine because there are thousands of other programmers using the same tools to build a large variety of projects.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Discontinuing%20Resourcelogic&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F10%2F06%2Fdiscontinuing-resourcelogic%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/10/06/discontinuing-resourcelogic/#comments" thr:count="24"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/10/06/discontinuing-resourcelogic/feed/atom/" thr:count="24"/>
		<thr:total>24</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Apologies to those needing help with my libraries]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/09/02/apologies-to-those-needing-help-with-my-libraries/" />
		<id>http://www.binarylogic.com/?p=801</id>
		<updated>2009-09-02T05:01:30Z</updated>
		<published>2009-09-02T04:56:48Z</published>
		<category scheme="http://www.binarylogic.com" term="General" />		<summary type="html"><![CDATA[I wanted to make a post about this. I&#8217;ve been extremely busy with my job lately. I&#8217;m launching a new company, and as we approach launch date it seems to get more hectic. I&#8217;ve got quite a few messages in my github account, email, google groups, etc. I usually try to block out about an [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/09/02/apologies-to-those-needing-help-with-my-libraries/"><![CDATA[<p>I wanted to make a post about this. I&#8217;ve been extremely busy with my job lately. I&#8217;m launching a new company, and as we approach launch date it seems to get more hectic. I&#8217;ve got quite a few messages in my github account, email, google groups, etc. I usually try to block out about an hour a day answering emails and performing basic chores with my open source projects, but I literally haven&#8217;t had an hour to spare. The time that I do have is spent maintaining my open source projects, so solving individual problems has been put on hold for now. I apologize if I haven&#8217;t responded to you. Don&#8217;t take it as an insult, I am just very busy right now. If you need help, please use google groups, the people on there are very helpful. By the way, thanks to everyone on google groups, github, Ryan Bates on railscasts, etc, for helping out. You guys really have been a big help, this is why I love open source.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Apologies%20to%20those%20needing%20help%20with%20my%20libraries&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F09%2F02%2Fapologies-to-those-needing-help-with-my-libraries%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/09/02/apologies-to-those-needing-help-with-my-libraries/#comments" thr:count="0"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/09/02/apologies-to-those-needing-help-with-my-libraries/feed/atom/" thr:count="0"/>
		<thr:total>0</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Using Searchlogic to combine named scopes with &#8216;OR&#8217;]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/08/26/using-searchlogic-to-combine-named-scopes-with-or/" />
		<id>http://www.binarylogic.com/?p=796</id>
		<updated>2009-08-26T08:21:47Z</updated>
		<published>2009-08-26T07:47:14Z</published>
		<category scheme="http://www.binarylogic.com" term="Searchlogic" /><category scheme="http://www.binarylogic.com" term="named scope procedure" /><category scheme="http://www.binarylogic.com" term="or" /><category scheme="http://www.binarylogic.com" term="scope procedure" />		<summary type="html"><![CDATA[A few days ago I released a feature that Ryan Bates requested: combining named scopes with &#8216;OR&#8217;. As you know, when you combine named scopes they join the conditions with &#8216;AND&#8217;. This obviously makes sense, but what if you want to combine conditions with &#8216;OR&#8217;? With this new feature you can do the following:

User.first_name_or_last_name_like("ben")
User.id_lt_or_age_gt(10)

I think [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/08/26/using-searchlogic-to-combine-named-scopes-with-or/"><![CDATA[<p>A few days ago I released <a href="http://github.com/binarylogic/searchlogic/issues/closed#issue/20">a feature that Ryan Bates requested</a>: combining named scopes with &#8216;OR&#8217;. As you know, when you combine named scopes they join the conditions with &#8216;AND&#8217;. This obviously makes sense, but what if you want to combine conditions with &#8216;OR&#8217;? With this new feature you can do the following:</p>
<pre class="ruby">
User.first_name_or_last_name_like("ben")
User.id_lt_or_age_gt(10)
</pre>
<p>I think this is nice feature on a variety of levels, but what I particularly like is the implementation.<span id="more-796"></span> It uses method_missing to accomplish this. You can call any scope you want, call scopes provided by <a href="http://github.com/binarylogic/searchlogic/tree/master">searchlogic</a>, call your own scopes, call scopes on associations, etc. As long as it maps to a named scope it will join the conditions with &#8216;OR&#8217;. So you have a little more flexibility when building a query with named scopes:</p>
<pre class="ruby">
User.id_gt(10).first_name_or_last_name_like("ben")
# => (id > 10) AND (first_name LIKE '%ben% OR last_name LIKE '%ben%')
</pre>
<h2>More than just named scopes</h2>
<p>A lot of people think <a href="http://github.com/binarylogic/searchlogic/tree/master">searchlogic</a> is just a bunch of named scopes, what people don&#8217;t realize is that it provides tools around how you use named scopes in general.</p>
<p>The other day I was reviewing an application for a friend who was using <a href="http://github.com/binarylogic/searchlogic/tree/master">searchlogic</a>. I noticed this:</p>
<pre class="ruby">
User.named_scope :with_recent_orders, :conditions => ["orders.created_at >= ?", 1.week.ago], :joins => :orders
Order.named_scope :recent, :conditions => ["orders.created_at >= ?", 1.week.ago]
</pre>
<p>See the duplication there? With more complex named scopes this becomes more obvious, but the scope was so simple I think he missed it. I even catch myself doing this sometimes, then I realize I don&#8217;t need to. The named_scope in the User class is pointless. With searchlogic, you can accomplish the same thing by doing:</p>
<pre class="ruby">
User.orders_recent
</pre>
<p><a href="http://github.com/binarylogic/searchlogic/tree/master">searchlogic</a> dynamically creates the scope for you, pulls over the scope details, and create the necessary join.</p>
<h2>The goal of named scopes</h2>
<p>In my opinion the simpler the named scope, the better. If you find yourself writing a complicated named scope, take a step back and look at it. I&#8217;d be willing to bet you could split that named scope into multiple smaller scopes. For example, instead of calling a named scope &#8216;Product.popular_expensive&#8217;, create 2 named scopes called &#8216;popular&#8217; and &#8216;expensive&#8217;. Break your scope into small parts and then piece them together when you need them.</p>
<p>Another good idea is to create scope procedures. This allows you to create small named scopes and still get the advantage of calling a single scope. Creating a procedure is as easy as creating a class level method:</p>
<pre class="ruby">
class User
  def self.awesome
    name_begins_with("Ben").name_ends_with("Johnson")
  end
end
</pre>
<p>Even better, used searchlogic&#8217;s scope procedure:</p>
<pre class="ruby">
User.scope_procedure :awesome, lambda { name_begins_with("Ben").name_ends_with("Johnson") }
</pre>
<p>I&#8217;ll leave it at that. If you are bored one day checkout the <a href="http://rdoc.info/projects/binarylogic/searchlogic">searchlogic docs</a>. I think you&#8217;ll find some small things like this that could help save you time and DRY up your code.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Using%20Searchlogic%20to%20combine%20named%20scopes%20with%20%26%238216%3BOR%26%238217%3B&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F08%2F26%2Fusing-searchlogic-to-combine-named-scopes-with-or%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/08/26/using-searchlogic-to-combine-named-scopes-with-or/#comments" thr:count="8"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/08/26/using-searchlogic-to-combine-named-scopes-with-or/feed/atom/" thr:count="8"/>
		<thr:total>8</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Heroku rocked my face off]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/08/25/heroku-rocked-my-face-off/" />
		<id>http://www.binarylogic.com/?p=792</id>
		<updated>2009-08-26T07:56:58Z</updated>
		<published>2009-08-25T08:58:52Z</published>
		<category scheme="http://www.binarylogic.com" term="General" /><category scheme="http://www.binarylogic.com" term="Uncategorized" /><category scheme="http://www.binarylogic.com" term="heroku" />		<summary type="html"><![CDATA[This week my task at hand was to figure out a deployment strategy for an application I&#8217;ve been working on. The initial conclusion was that we needed to buy another server. I wasn&#8217;t too happy with this because setting up a new server is a pain in the ass. It&#8217;s very repetitive, mundane, and boring. [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/08/25/heroku-rocked-my-face-off/"><![CDATA[<p>This week my task at hand was to figure out a deployment strategy for an application I&#8217;ve been working on. The initial conclusion was that we needed to buy another server. I wasn&#8217;t too happy with this because setting up a new server is a pain in the ass. It&#8217;s very repetitive, mundane, and boring. I&#8217;d much rather be spending time coding or coming up with new ideas instead of maintaining servers.</p>
<p>That being said, I figured I would see what <a href="http://www.engineyard.com">engine yard</a> or <a href="http://www.heroku.com">heroku</a> had to offer. Maybe it&#8217;s time my company starts using these services. It would be easier and cheaper.<span id="more-792"></span></p>
<h2>Engine Yard Solo</h2>
<p>Initially I was interested in engine yard&#8217;s solo plan. It seemed nice. It was a little confusing at first, but only took about an hour of playing around with it to really understand how it works. An hour is a small price to pay for something like this. Their stack was nice, not the stack I would choose, but who the hell cares at this point. I&#8217;m not going to start splitting hairs over using passenger or thin. The only thing I wasn&#8217;t too happy with was the pricing. To run a staging environment on their smallest plan would be around $115 a month. Then a production environment on top of this would be about $300. You&#8217;re looking at $415 a month to run a decent application. Is that a lot of money? Not for an important application, but my company also has a lot of small applications. We have blogs, small intranet apps, etc. That could start to add up, to the point where it would be more expensive than my current set up.</p>
<h2>Heroku</h2>
<p>Then I decided to checkout Heroku. The first thing I noticed is that their basic plan is free. You can host an application on there for free to start, which is AWESOME. I love that they do this, and in my opinion this is going to play a big part in their success. I signed up, followed the instructions they gave me, and had an application running in under 5 minutes. It was unbelievable how easy they made it. Literally, you create an application on Heroku, push your code to their git repository, and it deploys the app for you. I sat there for a minute, in awe, trying to soak it all in. It was awesome, but with all of this magic going on there has to be a catch or limitations. So I started to read the docs to try and understand it. To my surprise, there were a lot of advantages to using their set up, specifically around caching. There were also a few limitations, but nothing that I can&#8217;t live without. I won&#8217;t ramble on about the details because a lot of the issues are subjective. For example, is using PostgreSQL a limitation to you? What about a read-only filesystem? I&#8217;ve always been a MySQL guy, but after doing some research on PostgreSQL I really like it, and am going to make it my default choice for future apps.</p>
<h2>Conclusion</h2>
<p>If you haven&#8217;t tried out <a href="http://www.heroku.com">heroku</a> then you should. It takes about 10 minutes to try out, it&#8217;s free, and it&#8217;s well worth the time. Even if you know it&#8217;s not an option for you, its still fun to play around with. I have a feeling heroku is going to really take off and become a standard for ruby web applications. There are still some loose ends they need to tie up, but once they do there really is no reason not to use their service.</p>
<p>Also, no, I am not getting paid by Heroku. I wish I was though. I just like to share things on here that make my life easier, and this definitely did.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Heroku%20rocked%20my%20face%20off&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F08%2F25%2Fheroku-rocked-my-face-off%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/08/25/heroku-rocked-my-face-off/#comments" thr:count="18"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/08/25/heroku-rocked-my-face-off/feed/atom/" thr:count="18"/>
		<thr:total>18</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Authlogic ideas]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/08/13/authlogic-ideas/" />
		<id>http://www.binarylogic.com/?p=785</id>
		<updated>2009-08-13T20:00:00Z</updated>
		<published>2009-08-13T19:57:55Z</published>
		<category scheme="http://www.binarylogic.com" term="Authlogic" />		<summary type="html"><![CDATA[So I&#8217;ve been thinking about Authlogic lately and here are a few ideas I&#8217;ve been bouncing around:

Split out authenticating into &#8220;authenticators&#8221; that extend a class that acts as an interface, which would obviously be provided by Authlogic.So you would have a cookies authenticator, session authenticator, params authenticator, twitter oauth authenticator, openid authenticator, anything you want. [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/08/13/authlogic-ideas/"><![CDATA[<p>So I&#8217;ve been thinking about Authlogic lately and here are a few ideas I&#8217;ve been bouncing around:</p>
<ol>
<li style="margin-bottom: 20px"><strong>Split out authenticating into &#8220;authenticators&#8221; that extend a class that acts as an interface, which would obviously be provided by Authlogic.</strong><br />So you would have a cookies authenticator, session authenticator, params authenticator, twitter oauth authenticator, openid authenticator, anything you want. You could basically create an authenticator, register it with authlogic, and then you can do whatever you want in that class. This shouldn&#8217;t be a complicated task, more importantly I think it makes it easier to extend authlogic with different authentication methods.</li>
<li style="margin-bottom: 20px"><strong>Abstract the interaction with your models.</strong><br />This is especially hard because there is no standard between the different database libraries. But it would be nice to use Authlogic with DataMapper, MongoMapper, etc. I&#8217;m just hoping I can come up with a clean and clever way to do this. The only sure fire way that I can think of is to create adapters for each supported library, which means maintaing duplicate code. That would not be a fun task.
</li>
<li><strong>I&#8217;m toying with the idea of adding in common application code that you can include in your application.</strong><br />Such as resetting passwords, registering, logging in and out, grabbing the current user etc. What I don&#8217;t like about that is that now I am stepping into your application and making decisions for you. Is this a bad thing? I don&#8217;t know, thats for you to decide, but it definitely deviates a little bit from the &#8220;rails way&#8221;. Your UserSessionsController should look VERY similar to your other RESTful controllers. If you are willing to create those controllers / views, what&#8217;s the big deal with creating one for UserSessions? I just feel like it fits better. Here I have an application full of RESTful controllers where the code is pretty similar, then I have this UserSessionsController thats different. I just don&#8217;t like that. Also, what if you are using something like resource controller, inherited resources, or resourcelogic? What if you want your controller to use on of those libraries?
<p>The worst part about this idea are the views. Interfaces can be quite a bit different from application to application. How do I provide views that are suitable for every project? In my opinion you can&#8217;t. I could give you a million partials, tons of configuration, a lot of helpers, etc. This might add some flexibility, but this seems like a hack. It&#8217;s not a clean solution. This is the one thing that I just don&#8217;t like about rails engines. I prefer tools that help me create my views. Give me a set of tools so I can go easily create my interface, but don&#8217;t go create my interface for me. And I think rails does a pretty good job of this with things like form_for, etc.</li>
</ol>
<p>The bottom line is that I want to keep Authlogic focused on the business logic behind authentication. You should be able to use Authlogic in a rails app, merb app, sinatra app, etc. All of the interface cruft should probably be in a separate gem or in a template. The thing is, this gem could be written a million different ways depending on your preferences. Maybe I can create a base rails engine that people can fork and modify to their liking. That&#8217;s the beauty of git.</p>
<p>To conclude, #1 is probably going to happen, I want to do #2 if I can figure out a good way to do this, #3 is still up in the air and more likely to be in a separate gem / plugin.</p>
<p>That&#8217;s it for now, I figured I would try to keep everyone in the loop. Maybe you can help me out or have some ideas of your own.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Authlogic%20ideas&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F08%2F13%2Fauthlogic-ideas%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/08/13/authlogic-ideas/#comments" thr:count="12"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/08/13/authlogic-ideas/feed/atom/" thr:count="12"/>
		<thr:total>12</thr:total>
	</entry>
		<entry>
		<author>
			<name>benjohnson</name>
						<uri>http://www.binarylogic.com</uri>
					</author>
		<title type="html"><![CDATA[Introducing Shippinglogic]]></title>
		<link rel="alternate" type="text/html" href="http://www.binarylogic.com/2009/08/07/introducing-shippinglogic/" />
		<id>http://www.binarylogic.com/?p=774</id>
		<updated>2009-08-07T23:59:29Z</updated>
		<published>2009-08-07T23:59:29Z</published>
		<category scheme="http://www.binarylogic.com" term="Shippinglogic" /><category scheme="http://www.binarylogic.com" term="ruby" />		<summary type="html"><![CDATA[Today I released the first version of Shippinglogic, which is a ruby library I wrote to make integrating with FedEx, UPS, USPS, etc, very easy.
This is not a project I wanted to do, but I couldn&#8217;t find a library that did everything I needed it to. One library had support for creating shipments, but didn&#8217;t [...]]]></summary>
		<content type="html" xml:base="http://www.binarylogic.com/2009/08/07/introducing-shippinglogic/"><![CDATA[<p>Today I released the first version of <strong><a href="http://github.com/binarylogic/shippinglogic/tree/master" target="_blank">Shippinglogic</a></strong>, which is a ruby library I wrote to make integrating with FedEx, UPS, USPS, etc, very easy.</p>
<p>This is not a project I wanted to do, but I couldn&#8217;t find a library that did everything I needed it to. One library had support for creating shipments, but didn&#8217;t have support for getting a list of rates. Another had support for getting rates, but didn&#8217;t have support for creating shipments. I need both for the application I&#8217;m working on. I even decided to modify one of the libraries to suit my needs but the code was really hard to follow and I didn&#8217;t feel comfortable using it. Especially since shipping was such an important part of my application.</p>
<p>That&#8217;s when I came to the sad realization that I probably need to write my own library.<span id="more-774"></span> I was really surprised by the lack of choices when it came to shipping API integration. Then I started reading the FedEx documentation and realized why. Documentation on creating a shipment with the FedEx web services was 570 pages long. The amount of bullshit in the documentation astounded me. 99% of developers are probably concerned with maybe 30 pages in that entire document. I felt like I was reading a legal document, not web service documentation.</p>
<p>Sorry for the ranting, but this week has been especially frustrating, imagine reading FedEx documentation all day. Anyways, onto <strong><a href="http://github.com/binarylogic/shippinglogic/tree/master" target="_blank">Shippinglogic</a></strong>.</p>
<h3>What is Shippinglogic?</h3>
<p>My goal with <strong><a href="http://github.com/binarylogic/shippinglogic/tree/master" target="_blank">Shippinglogic</a></strong> was to make integrating with the various shipping APIs (FedEx, UPS, USPS, etc) easy and <strong>complete</strong>. The keyword being complete. Shippinglogic is not an abstraction layer, meaning each classes&#8217; focus is on the web service at hand. It is not trying to conform to an interface. With all of the differences between the various APIs its simply not possible to do this without severely limiting the feature sets for each API. There is no defined shipping standard that all of these companies agreed on, so how do you safely make an abstraction layer for this?</p>
<p>Anyways, I think it turned out pretty well. The syntax is pretty slick, thanks to the <a href="http://www.binarylogic.com/2009/08/07/how-to-create-a-proxy-class-in-ruby">proxy class trick I talked about in a previous post</a>.</p>
<p>Here&#8217;s a quick example of tracking a package:</p>
<pre class="ruby">
fedex = Shippinglogic::FedEx.new(key, password, account, meter)
tracking = fedex.track(:tracking_number => "XXXXXXXXXXXXXXXXX")

# this shows that the tracking object is actually a proxy for the underlying array
tracking.tracking_number
# => "XXXXXXXXXXXXXXXXX"

tracking.tracking_number = "YYYYYYYYYYYYYYY"
# => "YYYYYYYYYYYYYYY"

tracking.class
# => Array

tracking.first
# => #<Shippinglogic::FedEx::Track::Event @postal_code="95817", @name="Delivered", @state="CA", @residential=false,
#    @city="Sacramento", @type="DL", @country="US", @occured_at=Mon Dec 08 10:43:37 -0500 2008>

tracking.first.name
# => "Delivered"
</pre>
<p>I was pretty thorough with the documentation. Check out the <a href="http://github.com/binarylogic/shippinglogic/tree/master" target="_blank">project on github</a> for more examples. Let me know what you think.</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?sitename=Binary%20Logic&amp;siteurl=http%3A%2F%2Fwww.binarylogic.com%2F&amp;linkname=Introducing%20Shippinglogic&amp;linkurl=http%3A%2F%2Fwww.binarylogic.com%2F2009%2F08%2F07%2Fintroducing-shippinglogic%2F"><img src="http://www.binarylogic.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Save/Bookmark"/></a>]]></content>
		<link rel="replies" type="text/html" href="http://www.binarylogic.com/2009/08/07/introducing-shippinglogic/#comments" thr:count="20"/>
		<link rel="replies" type="application/atom+xml" href="http://www.binarylogic.com/2009/08/07/introducing-shippinglogic/feed/atom/" thr:count="20"/>
		<thr:total>20</thr:total>
	</entry>
	</feed>
