inicio mail me! sindicaci;ón

Archive for Rails

Zero-config Rails und PHP mit puma-dev

Mit puma-dev lassen sich beliebig viele Rack/Rails-Anwendungen durch Erstellen von Symlinks verfügbar machen.
Allerdings ist damit Port 80 blockiert und steht nicht mehr für Apache zur Verfügung.

Zum Glück erlaubt puma-dev nicht nur das Erstellen von Symlinks auf Rack-Apps sondern auch das Erstellen von Files innerhalb von ~/.puma-dev/, die als Inhalt einen alternativen Port enthalten.
Damit kann man alle Applikationen, die statt von puma-dev von Apache behandelt werden sollen, z.B. PHP-Apps und statische Seiten, als Files anlegen, die nur 81 enthalten.
Außerdem muss man Apache auf Port 81 lauschen lassen.

Die erforderlichen Schritte sind:

Apache-Konfiguration /etc/apache2/httpd.conf

Apache soll zukünftig auf Port 80 lauschen:

Listen 81

Die Module für virtuelle Hosts, Rewrite und PHP sollen verwendet werden:

LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php7_module libexec/apache2/libphp7.so
Include /private/etc/apache2/extra/httpd-vhosts.conf

Virtual Hosts Konfiguration /etc/apache2/extra/httpd-vhosts.conf

<VirtualHost 127.0.0.1:81>
  ServerName any.dev
  ServerAlias *.dev
  UseCanonicalName Off
  VirtualDocumentRoot "/Users/{username}/Sites/%1"
  <Directory "/Users/{username}/Sites/*">
    Options Indexes FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
    Require all granted
  </Directory>
</VirtualHost>

Angenommen die gewünschte PHP-Applikation liegt im Verzeichnis /Users/{username}/Sites/meine-app/.
Erstellt man eine Datei ~/.puma-dev/meine-app mit dem Inhalt 81, dann wird diese unter http://meine-app.test durch Apache verfügbar gemacht.

Happy hacking!

Using throw and catch to tidy up our code

Let’s say we want a simple controller action that checks, if a given code is valid.
It should return true and false and, if the code is invalid, give the reason (whether that is because it is unknown or because it has been used already). The action could look like this:

[gist id=2235608 bump=1]

Now this deep nesting effectively hides the underlying algorithm, a simple one in this case.
Expressing this using throw and catch straightens the code a bit:

[gist id=2235614 bump=1]

We are down to one respond_with line but the has merging and the throws at the beginning of the line are not particularly pretty.

Let’s introduce a little Ruby mixin for the Hash class that provides it with a compact method that its friend Array has had all along:

[gist id=2235631]

Now using this to clean up the response hash and moving the throw statements to the end so the steps of the algorithm are visible we have our final version of the action:

[gist id=2235618]

Saving local text files to S3 using paperclip

Sometimes you need to save a locally created file to S3 instead of an uploaded file (the standard). Here is how:

has_attached_file :tagged_text_file,  STORAGE_OPTIONS.merge({
  :processors   => []
})

...

def save_tagged_text_file
  file = File.open("#{RAILS_ROOT}/tmp/tagged_text_#{id}.txt", 'w+')
  file << tagged_text
  self.tagged_text_file = file
  save!
  file.close
end