Is it possible to "watch" Haml files in Compass (stylesheet authoring tool)? - haml

Currently, Compass is watching the .scss files inside the src folder and automatically updating the cs files. (by typing compass watch myproject).
Is there any way of including haml files in the "watching process"?
(I couldn't install StaticMatic because I don't want to install Ruby1.8).

Firstly, you should be using RVM. Installing any version of Ruby becomes painless.
Secondly, I just wrote this for myself, using the same 'fssm' gem that Compass uses to watch files. Add this to your / create a Rakefile:
require 'rubygems'
require 'fssm'
require 'haml'
class HamlWatcher
class << self
def watch
refresh
puts ">>> HamlWatcher is watching for changes. Press Ctrl-C to Stop."
FSSM.monitor('haml', '**/*.haml') do
update do |base, relative|
puts ">>> Change detected to: #{relative}"
HamlWatcher.compile(relative)
end
create do |base, relative|
puts ">>> File created: #{relative}"
HamlWatcher.compile(relative)
end
delete do |base, relative|
puts ">>> File deleted: #{relative}"
HamlWatcher.remove(relative)
end
end
end
def output_file(filename)
# './haml' retains the base directory structure
filename.gsub(/\.html\.haml$/,'.html')
end
def remove(file)
output = output_file(file)
File.delete output
puts "\033[0;31m remove\033[0m #{output}"
end
def compile(file)
output_file_name = output_file(file)
origin = File.open(File.join('haml', file)).read
result = Haml::Engine.new(origin).render
raise "Nothing rendered!" if result.empty?
# Write rendered HTML to file
color, action = File.exist?(output_file_name) ? [33, 'overwrite'] : [32, ' create']
puts "\033[0;#{color}m#{action}\033[0m #{output_file_name}"
File.open(output_file_name,'w') {|f| f.write(result)}
end
# Check that all haml templates have been rendered.
def refresh
Dir.glob('haml/**/*.haml').each do |file|
file.gsub!(/^haml\//, '')
compile(file) unless File.exist?(output_file(file))
end
end
end
end
namespace :haml do
desc "Watch the site's HAML templates and recompile them when they change"
task :watch do
require File.join(File.dirname(__FILE__), 'lib', 'haml_watcher')
HamlWatcher.watch
end
end
Run it with:
rake haml:watch

Related

Defining my Custom devise authentication strategy throws 'load_missing_constant' error

In my app, I am trying to define a custom strategy but it throws some errors... Here's what I am doing:
# myrailsapp/lib/devise/strategies
require 'devise/strategies/database_authenticatable'
module Devise
module Strategies
class CustomAuth < Devise::Strategies::DatabaseAuthenticatable
def valid?
Rails.logger.info("\n\nIn CustomAuth.valid\n\n")
true
end
def authenticate!
Rails.logger.info("\n\nCalled My Authenticate!\n\n")
super
end
end
end
end
Warden::Strategies.add(:custom_auth, Devise::Strategies::CustomAuth)
# config/initializer.rb
config.warden do |manager|
# manager.strategies.add(:custom_auth, Devise::Strategies::CustomAuth)
manager.default_strategies(:scope => :user).unshift :custom_auth
end
When I try to run this, I get this error:
/usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:490:in load_missing_constant': Expected app/helpers/custom_auth.rb to define CustomAuth (LoadError)
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:181:inconst_missing'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:179:in each'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:179:inconst_missing'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:501:in load_missing_constant'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:181:inconst_missing'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:179:in each'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:179:inconst_missing'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:501:in load_missing_constant'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:181:inconst_missing'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:179:in each'
from /usr/lib/ruby/gems/1.8/gems/activesupport-3.1.1/lib/active_support/dependencies.rb:179:inconst_missing'
from /home/rjain/src/qbol/tapp/config/initializers/devise.rb:212
Now, when I move the same file to my app/helpers directory, I get the same error, but if I remove the module Devise and module Strategies, things start to work. Two questions:
How to ensure that I can create the file in my lib/devise/strategies folder?
How to make it work so that I can use it within Devise::Strategies module as I described above?

Allow my own class in Rails to be configured with block syntax

I often see code of the form
RSpec.configure do |config|
config.include ApiHelper
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.include FactoryGirl::Syntax::Methods
end
Is there a feature in Rails that lets me do something similar? I want to be able to configure my own library with a similar syntax within config/initializers/my_class.rb
MyClass.configure do |config|
# allow configuration here
end
Nothing special is needed in Rails - it is simple Ruby code. Here is how it can be done:
class MyClass
def self.configure(&block)
a_hash = { :car => "Red" }
puts "Hello"
yield a_hash
puts "There"
end
end
MyClass.configure do |config|
puts "In Block"
puts config[:car]
end
Output:
Hello
In Block
Red
There
I am yielding a hash, but you can yield whatever object you want to.
Rails will load all the Ruby files in the config/initializers directory when starting up the server.
If you want to use the same style for your own custom configurable class then you just have to implement a configure class method that accepts a block and passes a configuration object to that block. e.g.
class MyClassConfiguration
# configuration attributes
end
class MyClass
def self.configure
yield configuration if block_given?
end
def self.configuration
#config ||= MyClassConfiguration.new
end
end
Using phoet's gem would be even easier.
Its worth taking a look at how RSpec does it if you are curious:
The RSpec.configure method is in https://github.com/rspec/rspec-core/blob/master/lib/rspec/core.rb
The Configuration class is implemented in https://github.com/rspec/rspec-core/blob/master/lib/rspec/core/configuration.rb
i don't know if rails provides a helper for that, but i wrote my own tiny solution for this problem that i use in several gems: https://github.com/phoet/confiture
it let's you define configurations:
module Your
class Configuration
include Confiture::Configuration
confiture_allowed_keys(:secret, :key)
confiture_defaults(secret: 'SECRET_STUFF', key: 'EVEN_MOAR_SECRET')
end
end
and have an easy api to do the configuration:
Your::Configuration.configure do |config|
config.secret = 'your-secret'
config.key = 'your-key'
end
besides this, there are a lot of other config tools out there like configatron or simpleconfig.
A global configuration set via the block.
module VkRobioAPI
module Configuration
OPTION_NAMES = [
:client_id,
:redirect_uri,
:display,
:response_type
]
attr_accessor *OPTION_NAMES
def configure
yield self if block_given?
self
end
end
end
module VkRobioAPI
extend VkRobioAPI::Configuration
class << self
def result
puts VkRobioAPI.client_id
end
end
end
Example:
VkRobioAPI.configure do |config|
config.client_id = '3427211'
config.redirect_uri = 'bEWLUZrNLxff1oQpEa6M'
config.response_type = 'http://localhost:3000/oauth/callback'
config.display = 'token'
end
Result:
VkRobioAPI.result
#3427211
#=> nil
If you are using rails, you can just include ActiveSupport::Configurable into your class and off you go. Outside of rails, you will have to add the activesuport gem to your Gemfile or gemspec and then call require 'active_support/configurable'.
Example
class MyClass
include ActiveSupport::Configurable
end
Usage
With block
MyClass.configure do |config|
config.key = "something"
config.key2 = "something else"
end
or inline, without block
MyClass.config.key = "something"
MyClass.config.key2 = "something else"
or like a hash
MyClass.config[:key] = "somehting"
MyClass.config[:key2] = "something else"
Note: key can be any character of your choice.

Why after_commit not running even with use_transactional_fixtures = false

Transactional fixtures in rspec prevent after_commit from being called, but even when I disable them with
RSpec.configure do |config|
config.use_transactional_fixtures = false
end
The after_commit callback does not run.
Here is a rails app with the latest rspec / rails that I have produced the issue on:
git://github.com/sheabarton/after_commit_demo.git
One way around this is to trigger the commit callbacks manually. Example:
describe SomeModel do
subject { ... }
context 'after_commit' do
after { subject.run_callbacks(:commit) }
it 'does something' do
subject.should_receive(:some_message)
end
end
end
A little late, but hope this helps others.
In my case I resolved such problem with database_cleaner's settings placed below:
config.use_transactional_fixtures = false
config.before(:suite) do
DatabaseCleaner.strategy = :deletion
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
Thanks to Testing after_commit/after_transaction with Rspec
This is similar to #jamesdevar's answer above, but I couldn't add a code block, so I have to make a separate entry.
You don't have the change the strategy for the whole spec suite. You can keep using :transaction globally then just use :deletion or :truncation (they both work) as needed. Just add a flag to the relevant spec.
config.use_transactional_fixtures = false
config.before(:suite) do
# The :transaction strategy prevents :after_commit hooks from running
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each, :with_after_commit => true) do
DatabaseCleaner.strategy = :truncation
end
then, in your specs:
describe "some test requiring after_commit hooks", :with_after_commit => true do
If you're using database_cleaner you'll still run into this. I'm using the test_after_commit gem, and that seems to do the trick for me.
This Gist helped me.
It monkey-patches ActiveRecord to fire after_commit callbacks even if using transactional fixtures.
module ActiveRecord
module ConnectionAdapters
module DatabaseStatements
#
# Run the normal transaction method; when it's done, check to see if there
# is exactly one open transaction. If so, that's the transactional
# fixtures transaction; from the model's standpoint, the completed
# transaction is the real deal. Send commit callbacks to models.
#
# If the transaction block raises a Rollback, we need to know, so we don't
# call the commit hooks. Other exceptions don't need to be explicitly
# accounted for since they will raise uncaught through this method and
# prevent the code after the hook from running.
#
def transaction_with_transactional_fixtures(options = {}, &block)
rolled_back = false
transaction_without_transactional_fixtures do
begin
yield
rescue ActiveRecord::Rollback => e
rolled_back = true
raise e
end
end
if !rolled_back && open_transactions == 1
commit_transaction_records(false)
end
end
alias_method_chain :transaction, :transactional_fixtures
#
# The #_current_transaction_records is an stack of arrays, each one
# containing the records associated with the corresponding transaction
# in the transaction stack. This is used by the
# `rollback_transaction_records` method (to only send a rollback hook to
# models attached to the transaction being rolled back) but is usually
# ignored by the `commit_transaction_records` method. Here we
# monkey-patch it to temporarily replace the array with only the records
# for the top-of-stack transaction, so the real
# `commit_transaction_records` method only sends callbacks to those.
#
def commit_transaction_records_with_transactional_fixtures(commit = true)
unless commit
real_current_transaction_records = #_current_transaction_records
#_current_transaction_records = #_current_transaction_records.pop
end
begin
commit_transaction_records_without_transactional_fixtures
rescue # works better with that :)
ensure
unless commit
#_current_transaction_records = real_current_transaction_records
end
end
end
alias_method_chain :commit_transaction_records, :transactional_fixtures
end
end
end
Put this a new file in your Rails.root/spec/support directory, e.g. spec/support/after_commit_with_transactional_fixtures.rb.
Rails 3 will automatically load it in the test environment.

rails-translate-routes gem: Is it possible to translate routes but keep (some) original ones?

I'm using rails-translate-routes gem to translate "front" routes only.
I'm using carrierwave to upload some files in my admin. Here's an uploader:
class CheatsheetUploader < CarrierWave::Uploader::Base
[...]
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
[...]
end
As you see, the path is using the name of the model and the name of the field.
When I try to get the file:
link_to "my file", download.cheatsheet.url
The path is the default one:
http://localhost:3000/uploads/download/cheatsheet/1/a_really_nice_file.pdf
And Rails give me a nice Routing error:
No route matches [GET] "/uploads/download/cheatsheet/1/a_really_nice_file.pdf"
Any way to handle this?
You can pass keep_untranslated_routes: true in your call to ActionDispatch::Routing::Translator.translate_from_file.
For example:
ActionDispatch::Routing::Translator.translate_from_file(
'config/locales/routes.yml',
no_prefixes: true,
keep_untranslated_routes: true)

Converting to modular sinatra app breaks tests

I've modularised by classic sinatra app and moved my routes in my sinatra app into individual routes files as per https://stackoverflow.com/a/5030173/111884, however, I can't seem to get my tests working.
This is what my files look like:
./web.rb
require 'sinatra'
require 'sinatra/flash'
class MyApp < Sinatra::Application
# ...
end
require_relative 'models/init'
require_relative 'helpers/init'
require_relative 'routes/init'
./routes/init.rb
require_relative 'main'
./routes/main.rb
# The main routes for the core of the app
class MyApp < Sinatra::Application
get '/' do
erb :main
end
end
./spec/spec_helper.rb
ENV['RACK_ENV'] = 'test'
require 'minitest/autorun'
require 'rack/test'
require 'factory_girl'
# Include factories.rb file
begin
require_relative '../test/factories.rb'
rescue NameError
require File.expand_path('../test/factories.rb', __FILE__)
end
# Include web.rb file
begin
require_relative '../web'
rescue NameError
require File.expand_path('../web', __FILE__)
end
./spec/web_spec.rb
begin
require_relative 'spec_helper'
rescue NameError
require File.expand_path('spec_helper', __FILE__)
end
include Rack::Test::Methods
def app() Sinatra::Base end
describe "Some test" do
# ...
end
Rakefile
# Test rake tasks
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.libs << "spec"
t.test_files = FileList['test/factories.rb', 'test/test_*.rb', 'spec/spec_helper.rb', 'spec/**/*_spec.rb']
t.verbose = true
end
The output of the tests is:
<h1>Not Found</h1>
It doesn't seem to be loading the ./routes/*.rb files.
I'm using Sinatra::Application, instead of Sinatra::Base, but https://stackoverflow.com/a/5030173/111884 uses it. It also references it here http://www.sinatrarb.com/extensions.html. I have tried changing it to use Sinatra::Base, but it didn't fix it.
I've also tried Sinatra tests always 404'ing and Using Cucumber With Modular Sinatra Apps, but they don't work.
I think you just need to change your app method to return your modularised application class (MyApp) rather than the Sinatra::Base class. So replace:
def app() Sinatra::Base end
in web_spec.rb, with:
def app
MyApp
end
Rack::Test::Methods depends on the app method to tell it which class to call to process requests. In a simple non-modular Sinatra application that class is Sinatra::Base because that is the class in which the routes are applied by default. In a modular application that is the class in which you define your routes (MyApp in your case).