lucasefe / themes_for_rails

Theme Support for Rails 3
This very same page :)
MIT License
308 stars 102 forks source link

SASS support #11

Closed farnoy closed 13 years ago

farnoy commented 13 years ago

Sass in rails compiles templates in public/stylesheets. Some initializer to compile themes stylesheets in sass would be great.

lucasefe commented 13 years ago

I would need to talk to the guys from SASS to see how to implement this. Do you have something in mind?

farnoy commented 13 years ago

Well, as far as I know, sass comes with haml and is initialized in rails with this code. After installing gem 'haml' from Gemfile, a file appears in vendor/plugins/haml.rb. I don't know how you initialize and handle haml, but this should do perfectly as a reference.

lucasefe commented 13 years ago

You are mixing concepts there. When you use haml as a gem, you don't have the gem in vendor/plugins. But that's not the problem.

The thing is, what are you trying to achieve? We want to have a folder for sass templates inside the theme so SASS can find and compiles them as if they were inside public/stylesheets? Will a folder like RAILS_ROOT/themes/some_theme/sass work for you?

I think this is doable, but I will have to check Sass to know how to change the template_path on the fly (whenever you select a theme).

farnoy commented 13 years ago

I think that rails with sass compiles templates everytime they change when in development and once, at start in production, but it doesn't have to be in special directory. It compiles from everywhere under public/stylesheets/. But additional directory (like sass/) in theme's folder will be good too.

matheusmoreira commented 13 years ago

According to the Sass reference, :template_location may contain multiple paths.

I suggest keeping the files inside /themes/#{current_theme}/stylesheets/sass.

So, if we set the :css_location to the current theme's stylesheet folder, the template location will automatically default to css_location + '/sass':

Sass::Plugin.options[:css_location] = "themes/#{current_theme}/stylesheets"

The downside is that Sass will supposedly only watch and compile current_theme's stylesheets. It may also not work, as I'm not quite sure whether or not we can actually retrieve the current theme when this line is executed.

As an alternative, we could try adding every theme to the template path during initialization. Here's an (untested) proof of concept implementation:

# How about a ThemesForRails.each_theme method?
Dir.foreach Rails.root.join 'themes' do |dir|
  if File.directory? dir # Need to get rid of the '.' and '..'
    add_template_location "#{dir}/stylesheets/sass",
                          "#{dir}/stylesheets"
  end
end

This way:

I saw in the "Things to remember" section that you want to load the theme list during initialization in the future, and I think Sass compatibility can be easily factored into that.

lucasefe commented 13 years ago

Second approach is better to me. Check what I did. See the readme file and inside the project, the themes_for_rails.rb file.

Again, it is not available as gem yet. I want you to give it a try, would you? Let me know your thoughts.

Cheers

matheusmoreira commented 13 years ago

Wow, that was fast. :D

Just a quick update, bundle reported an error during bundle install:

themes_for_rails at /usr/local/rvm/gems/ruby-1.9.2-p136/bundler/gems/themes_for_rails-9d03bc142fbf did not have a valid gemspec.
This prevents bundler from installing bins or native extensions, but that may not affect its functionality.
The validation message from Rubygems was:
  ["lib/generators/theme_for_rails/install_generator.rb", "lib/generators/theme_for_rails/templates/theme/images/.gitkeep", "lib/generators/theme_for_rails/templates/theme/javascripts/.gitkeep", "lib/generators/theme_for_rails/templates/theme/stylesheets/.gitkeep", "lib/generators/theme_for_rails/templates/theme/views/layouts/.gitkeep", "lib/generators/theme_for_rails/theme_generator.rb"] are not files

It seems to have installed correctly nonetheless. I'm trying it out right now.

lucasefe commented 13 years ago

Fixed! Run bundler again! :)

matheusmoreira commented 13 years ago

Very nice.

I'm getting an error when calling theme_stylesheet_link_tag:

undefined method `base_theme_stylesheet_path' for #<#<Class:0x00000003358010>:0x00000003352d68>
lucasefe commented 13 years ago

Did you do themes_for_rails in your routes.rb file? Paste a snippet.

base_theme_stylesheet_path is the name of one of the provided routes.

matheusmoreira commented 13 years ago

... My bad. :S

lucasefe commented 13 years ago

Got you there. Are you trying this one new project? ;)

I think the themes_for_rails routes configuration step should be automatic. What do you think?

farnoy commented 13 years ago

It works, thanks for this. Compiles on each change when in development mode, sweet.

I think that configuration is useless here, routes should be automatic, aswell the config.themes_for_rails.use_sass = true line.

Close?

lucasefe commented 13 years ago

Can you wait a couple of minutes? I'll do those changes.

lucasefe commented 13 years ago

Done, please, pull. Works?

farnoy commented 13 years ago

Works without that line now, but still need to specify route.

lucasefe commented 13 years ago

I know, I am gonna leave that for now, mostly for compatibility concerns. But enabling sass by default, if available, makes totally sense.

Shall we close this issue?

farnoy commented 13 years ago

Yes, thanks again for sass support

lucasefe commented 13 years ago

Dude, glad to do it. Anytime.

matheusmoreira commented 13 years ago

Not yet. In development mode, every time I refresh the page, all the stylesheet directories get added to the template path again. After some time I have one huge list of duplicate entries.

This is just a memory leak that may not affect production code. Other than that, it is working wonderfully.

Also, what about projects that don't use Sass? Won't the attempt to configure it fail if it's not present? You should somehow detect it's presence and enable support if it's there.

farnoy commented 13 years ago

When you use haml gem in rails, even if you don't have any .sass|.scss files, it watches and compiles anyways.

matheusmoreira commented 13 years ago

Yes, but what if they aren't using the haml gem at all?

farnoy commented 13 years ago

Oh snap!

@lucasefe Have you used the defined? macro for implementing sass? Have anyone tested it without sass/haml?

lucasefe commented 13 years ago

If you don't use haml-rails gem, the constant Sass will not be available, then ThemesForRails will not try to do a thing. If you are including haml in your gemfile but you are not using it, then just set the config.themes_for_rails.use_sass to false.

Matheus, I fixed to development mode issue related to duplicated entries.

lucasefe commented 13 years ago

See this:

https://github.com/lucasefe/themes_for_rails/blob/master/lib/themes_for_rails/config.rb#L26

matheusmoreira commented 13 years ago

Edit - Nevermind, problem was on my end. Sorry.

lucasefe commented 13 years ago

The idea behind that method is to add the stylesheets theme path to sass only if it is not already available in the Sass configuration.

Can you explain me the error? What is doing wrong?

Anyway, I've changed the method a little, but functionality is the same.

matheusmoreira commented 13 years ago

It's working just fine. Problem was on me again. :S

Apologies.

lucasefe commented 13 years ago

No problem, Matheus.

Btw, the new gem has been published. Version 0.4.0.