Technology

has_slug arrives in a brave new world

Posted on 10/02/2009. Filed under: Technology | Tags: , |

A brave new world full of ->() block literals, ordered hashes and application templates. Of course I’m speaking of not only Ruby 1.9 but also Rails 2.3. Yes people, you’ve heard it here first: my glorious has_slug Rails plugin is now Ruby 1.9 as well as Rails 2.3 compatible.

One might argue that I’m a bit late with this but then again one might argue that green is the new pink. So yeah.. whatever!

Even though this might look like a pure maintenance/compatibility release, one other noteworthy change managed to sneak in: rescue_from_slug_mismatch is gone. Oh yes, stop the presses, I’m breaking backwards compatibility! I hope this doesn’t cause all the two users of has_slug too much trouble but sometimes you just gotta make those hard decisions. Be a good Rails citizen and use rescue_from SlugMismatchError instead. Kthx.

http://github.com/Nielsomat/has_slug/tree/master

Enjoy!

Advertisements
Read Full Post | Make a Comment ( None so far )

Introducing has_slug for Rails

Posted on 29/12/2008. Filed under: Technology | Tags: , |

Straight from README.rdoc:

has_slug

Deliciously slugalizing ActiveRecord & your ActionControllers since Rails 2.2

http://www.github.com/Nielsomat/has_slug

        ./script/plugin install git://github.com/Nielsomat/has_slug.git
        or git submodule add git://github.com/Nielsomat/has_slug.git vendor/plugins/has_slug

has_slug provides an easy way to equip your models with
“permalinks” like so:

        class BlogPost < ActiveRecord::Base
          has_slug(:category, :title)
        end

        post = BlogPost.create(:category => "Time Travelling", :title => "Yatta!")
        # => #<BlogPost id: 1, category: "Time Travelling", title: "Yatta!">

        post.to_param
        # => "1-time-travelling-yatta"

That‘s nice. But surely not revolutionary.

So let‘s turn our attention to the controller which should of course
be able to find the record identified by the above “permalink”.
Turns out that Rails kinda does this by default by virtue of calling to_i
on params[:id] when passing it to ActiveRecord. Easy enough..

But wait, there‘s more!

Of course this automatic conversion means that your carefully worded
“2-honey-you-look-great-in-that-dress” could also be accessed
under “2-youre-way-too-fat-for-that-dress-honey”. Not so good. In
fact, just anything matching /^2[W$]/ would lead to BlogPost.find(2).
Besides upsetting your wife and/or mistress, this might obvisouly also
annoy search engines which have a fetish for unique resource itentifiers
(i.e. URIs).

But fear not for it has all been taken care of for you!

        class BlogPostsController < ApplicationController
          rescue_from_slug_mismatch { |item| redirect_to(blog_post_url(item), :status => :moved_permanently) }

          def show
            check_slug!(@post = BlogPost.find(params[:id]), params[:id])
          end
        end

And behold: GET /blog_posts/2-honey-you-look-great-in-that-dress
will render /blog_posts/show.hmtl.erb while GET
/blog_posts/2-youre-way-too-fat-for-that-dress-honey
will 302
to /blog_posts/2-why-i-love-my-wife.

Even better though, the above example actually still works when written
like this:

        class BlogPostsController < ApplicationController
          def show
            check_slug!(@post = BlogPost.find(params[:id]))
          end
        end

Because awesomely enough, the second parameter to check_slug! defaults to
params[:id] and by default, all ApplicationController descendants are
lovingly wired to rescue you from SlugMismatchErrors by redirecting to
model_url(model).

So what are you waiting for? Just has_slug your models and
consider your application awesomified!

Copyright (c) 2008 Niels Ganser, released under the MIT license. Even
though this software is released “as is”, I accept full
responsibility for any bone fractures that might directly result from using
it. So in the case of such spontaneous fracturing, feel free contact my
armada of lawyers to discuss your ridiculously high compensation claims.

All your feature requests / bug reports / patches are belong to github!
Or niels at herimedia dot com..

Shout-out to Christoffer Sawicki & Henrik Nyh for creating Slugalizer,
upon which this plugin was originally based (obsolete since Rails 2.2)!

Read Full Post | Make a Comment ( 1 so far )

Arrested Splunkification (“No such method ‘getClassChksums'”)

Posted on 25/11/2008. Filed under: Technology | Tags: |

Getting errors like
WARN  PipelineComponentCommHandler - No such method 'getClassChksums'
on your Splunk deployment clients or
ERROR DistributedDeploymentClientMgr - error returned from getClassChksums: <?xml version="1.0"?>
<error>No such method name</error>

on your Splunk deployment server?

Well, you probably dont’t have an enterprise license then.. :)

Along with indexing more data than 500M/day this is one of the features restricted to paying customers. So you can either get your 30-day trial here and after a month start handing over >$xxxx,xx / year to Splunk or you can go and set up some NFS shares to make your logs available to your Splunk server which is what I am going to to right now..

Read Full Post | Make a Comment ( None so far )

[fixed] Mac OS X users lost administrator access

Posted on 22/03/2008. Filed under: Technology | Tags: |

So several people are reporting trouble with Leopard when after upgrading or e.g. shutting down by just holding the power button all users have lost administrator access they might have had before.

A very quick fix helps most people:

– Restart
– Hold down Apple + S when the first blue/greyish screen appears to boot into single user mode
– The hard drive is mounted read-only so we have to do
$ /sbin/fsck -fy
$ /sbin/mount -uw /
– Now the system’s standard root user (whom we are at the moment) gets a password (any will do) so we can log in with it
$ passwd
$ exit
– Click on “Other” in the Login Window and login as user “root” with the password you have just assigned to him
– Use “System Preferences” -> “Accounts” to give administrator access back to the users whom you want to have it
– Log out
– Log in with your newly re-appointed administrator, launch Directory Utility, authenticate by clicking on the lock icon and disable the root user via “Edit” -> “Disable Root User”

Most users live happily ever after but for some this simply doesn’t work. Specifically System Preferences seems to immediately “forget” that you want a specific user to be an administrator. You tick the box, you click on another user or click the lock and then when you click on your desired administrator again the box is unchecked once more.

The more curious among us will at this time already have checked the system logs where most likely the message “mbr_group_name_to_uuid failed with err=2” sticks out. At the usual places (discussions.apple.com, et al) people will want you to insert your installation disk, use a password utility, reboot repeatedly, even reinstall your OS X or just pray.

Well, welcome to panoramification where everything is different ;)

What we’ll do is the following:

– back to single user mode as described above, also do the fsck and mounting
1. load directory services:
$ launchctl load /System/Library/LaunchDaemons/com.apple.DirectoryServices.plist
2. Try to add a user to the admin group, use the shortname of the user for this
$ dscl . append /groups/admin GroupMembership myuser
– If this works: congratulations. Reboot and you’re done! If however it fails with
=> <dscl_cmd> DS Error: -14009 (eDSUnknownNodeName)
4. then try to create this group with its standard id of 80
$ dseditgroup -o create -i 80 admin
– if it works, skip to 9. If it fails with
=> ERROR: A Directory Service error occured.
14135: eDSRecordAlreadyExists
5. then try to delete the (apparently corrupted) group first
$ dseditgroup -o delete admin
– if this works, skip to 8. If it fails with the mind-blowing
=> ERROR: A Directory Service error occured.
14136: eDSRecordNotFound
6. we should check out the admin.plist file
$ cat /var/db/dslocal/nodes/Default/groups/admin.plist
7. if this file does not contain anything meaningful (should be an xml file) then the system is fooled into believing the group exists but can’t actually “initialize” it. We’ll remove the empty/corrupted file
$ rm /var/db/dslocal/nodes/Default/groups/admin.plist
8. … create a new group “from scratch”
$ dseditgroup -o create -i 80 admin
9. … and add our user to it
$ dscl . append /groups/admin GroupMembership myuser
10. All is done now, reboot and enjoy!
$ reboot
Of course an empty group.plist is a fairly trivial problem that any system created after the year 1996 should take care of automatically but well.. Apple can’t always be on the bleeding edge *cough*.

I’ve posted some more detail on this in the apple.com forums.

Feel free to leave a comment if any of this doesn’t help you out and I’ll look into it some more.

Read Full Post | Make a Comment ( 12 so far )

Macportified gem install mysql

Posted on 07/03/2008. Filed under: Technology | Tags: , , , |

So I finally decided to act upon this

WARNING: You're using the Ruby-based MySQL library that ships with Rails.
This library is not suited for production. Please install the C-based MySQL
library instead (gem install mysql).

message I kept getting on my new Macbook running Leopard 10.5.2.

I am using Macports’ MySQL5 port and of course the gem install failed with

ERROR:  Error installing mysql:
	ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb 
install mysql
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of
necessary libraries and/or headers.  Check the mkmf.log file for more
details.  You may need configuration options.

Having not been in the mood to dig deeper into it myself I consulted mighty Google which promptly came up with this post over at Chrises Code Intensity Blog describing which flags need to be set to what to make this work:

sudo env ARCHFLAGS="-arch i386" gem install mysql -- 
--with-mysql-include=/opt/local/include/mysql5 
--with-mysql-lib=/opt/local/lib/mysql5 
--with-mysql-config=/opt/local/lib/mysql5/bin/mysql_config

Easy! :)

Read Full Post | Make a Comment ( None so far )

acts_as_state_machine initial state instant save

Posted on 07/03/2008. Filed under: Technology | Tags: |

Beware when using the excellent acts_as_state_machine (AASM): the corresponding action to your initial state will be called AFTER creation of the record (i.e. it is added to the :after_create callbacks).

In particular consider the following:

 class User < ActiveRecord::Base

  acts_as_state_machine :initial => :active
  state :passive
  state :active,  :enter => :do_activate

  def do_activate
    self.activated_at = Time.now.utc
    self.deleted_at = self.activation_code = nil
  end

end u = User.create(attributes)
u.activated_at => nil

However, changing do_activate to include a call to save like this:

def do_activate
  self.activated_at = Time.now.utc
  self.deleted_at = self.activation_code = nil
  save # This is the all important change
end

Will work as expected (at least as I expected it to work..):

u = User.create(attributes)
u.activated_at => Fri Mar 07 16:59:05 UTC 2008
Read Full Post | Make a Comment ( None so far )

Liked it here?
Why not try sites on the blogroll...