“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust.
I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay who are based in Denmark...”
>> %x{ /etc/init.d/apache2 restart }
(13)Permission denied: make_sock: could not bind to address 0.0.0.0:80
no listening sockets available, shutting down
Unable to open logs
=> " * Restarting web server apache2\n ...fail!\n"
>> $?
=> #<Process::Status: pid=2016,exited(1)>
>> $?.exitstatus
=> 1
Just found a broken test that I just couldn’t fathom, its testing an action that occurs within in an callback. We need to set up an object in the correct state to be inline without our live database. new_salted_password is set when the record is next saved, so it is important that it is nil before we call our method.
This has worked fine for sometime. Write attribute appears to skip callbacks. I could have written the following as the callback actually occurs within an after_validation callback and the validation step is skipped here.
However, I needed to move the callback method within the before_save callback sequence so both of the above methods failed both save(false) and write_attribute saves the object. After much hunting it appears the solution is to the use the private method update_without_callbacks. There is also a create_without_callbacks method.
Just picked up a copy of Programming Erlang from Pragmatic Programmers, and having a quick play in the console.
The story so far;
lines are terminated by ‘.’
variables are not variables (not quite) can only be assigned once
= is not an assignment operator, it is a pattern matching operator
rl@bloodandguts:~$ erl
Erlang (BEAM) emulator version 5.6.3 [source] [64-bit] [smp:2] [async-threads:0] [kernel-poll:false]
Eshell V5.6.3 (abort with ^G)
1> % This is a comment
1> 123456.
123456
2> "a string".
"a string"
3> 3 + 4 * 6.
27
4> (3 + 4) * 6.
42
5> 16#cafe .
51966
6> 32#cafe .
403950
7> X = 12345.
12345
8> X * 3.
37035
9> X = "something else".
** exception error: no match of right hand side value "something else"
There have been several times recently when I’ve needed to test that the time set in a particular method is Time.now. However, this isn’t particularly (or at all) accurate.
s = Story.new
s.access_it
s.last_accessed_at.should == Time.now
This all reads fine, but our test will always take time to run. So the current time when then the method is called is not likely to be same as the current time that it is being tested at and if it is then you are extremely lucky. Hence the following.
Time.stub!(:now).and_return(Time.now)
s = Story.new
s.access_it
s.last_accessed_at.should == Time.now
I’m really not a fan of using GETs to call actions that create things like so.
<%= link_to image_tag('mini_icons/copy.png', :alt => 'Make a copy of this lesson'), copy_lesson_path(lesson), :method => :post %> <br/>
<%= link_to "Copy", copy_lesson_path(lesson), :method => :post %>
Despite using a form to pretend to be a link I much prefer this solution, because it actually enforces a POST to call the ‘copy’ action rather then pretending to be a post with a param { “_method” => “post” }
Ok so I decided to remove the bad version that was complaining.
sudo gem uninstall rspec -v 1.1.12
Gah, another error.
RubyGem version error: rspec(1.1.4 not = 1.1.12) (Gem::LoadError)
Ok so it must be something to do with version mismatches and there are always 2 gems required for RSpec with Rails. I guess rspec-rails looks for the nearest matched version of rspec
I’ve been musing over the past day or so on how best to assess whether our mailers are sending out messages that are likely to get caught by over zealous spam filters and render our e-mail campaigns useless.
One possible is to run all outbound e-mails through spam assassin, so for standard mailers we can just add a spec to check the content and for form mailers we can use it during validation before the final send off.
class TMail::Mail
def spam?
spam_filtered_body = %x[ echo '#{self.encoded}' | /usr/bin/spamassassin ]
return !! spam_filtered_body.match(/X-Spam-Status:\sYes/)
end
end
I’ve decided to go back to writing my own blog software. Simply Mephisto is too much for my needs and playing around with building my own Mephisto plugin just to create a plain old boring static page is just too time consuming. In fact I was able to knock up a new blog in less time.
My main issue with writing my own blog the last time was getting RedCloth to play nicely with CodeRay. Fortunately I stumbled upon the solution on GitHub.
It wasn’t quite what I wanted but gave me a good start and a few modifications later we have.
“./config/initializers/redcloth_with_coderay.rb”
module RedclothWithCoderay
SOURCE_TAG_REGEXP = /(<pre><code>(.+?)<\/source>)/m
# The RedCloth extension that performs the syntax highlighting.
def refs_syntax_highlighter(text)
text.gsub!(SOURCE_TAG_REGEXP) do |m|
all_of_it = $~[0]
lang = ($~[2] || :ruby).to_sym
code = $~[3].strip
highlighted = CodeRay.scan(code, lang).html(:wrap => :div, :css => :class, :line_numbers => :inline)
"<notextile>#{highlighted}</notextile>"
end
end
end
RedCloth.class_eval { include RedclothWithCoderay }
“./app/helpers/application_helper.rb”
module ApplicationHelper
def textilize(text)
RedCloth.new(text).to_html(:textile, :refs_syntax_highlighter)
end
end
Typically after getting this to work adding text after any code block was susequently hampered by a bug in RedCloth-4.1.9 which had me stumped for a whole day. I have reverted to RedCloth-4.1.1 in the meantime and its all lovely again.
It seems there are a few ways of grabbing the to address back, to just returns an array of e-mail addresses. to_addrs or [‘to’] actually return the full “to” address.