Giter VIP home page Giter VIP logo

themes_on_rails's Introduction

ThemesOnRails Build Status Dependency Status Code Climate Coverage Status Gem Version

Installation

The simplest way to install is to use Bundler.

Add this gem to your Gemfile:

gem 'themes_on_rails'

If you want to use themes_on_rails with liquid template, add one more gem to your Gemfile:

gem 'liquid-rails'

Then, use Bundler to install the gem and its dependencies:

$ bundle install

Usage

A theme is composed of three things:

  1. Assets: images, javascripts, stylesheets
  2. Views: templates and layouts (erb, haml, or other template engines)
  3. Locales: locales files if any

Generator

To generate theme inside your app:

$ rails g themes_on_rails:theme theme_name
app/
  themes/
    [theme_name]/
      assets/
        images/
          [theme_name]/
        stylesheets/
          [theme_name]/
            all.css
        javascripts/
          [theme_name]/
            all.js
      views/
        layouts/
          [theme_name].html.erb
      locales/

After you invoke the above command, make sure you restart your rails process.

It's best advisable to namespace your assets directory so that it won't conflict with assets in other themes.

image_tag              'theme_a/logo.png' # => app/themes/theme_a/assets/images/theme_a/logo.png
javascript_include_tag 'theme_a/all'      # => app/themes/theme_a/assets/javascripts/theme_a/all.js
stylesheet_link_tag    'theme_a/all'      # => app/themes/theme_a/assets/stylesheets/theme_a/all.css

There is an example app at https://github.com/chamnap/themes_on_rails_example.

Controller

You can set theme in your controllers by using the theme declaration. For example:

class HomeController < ApplicationController
  theme 'basic'

  def index
    ...
  end
end

With this declaration, all of the views rendered by the home controller will use app/themes/basic/views/home/index.html.erb as its templates and use app/themes/basic/views/layouts/basic.html.erb.

You can use a symbol to defer the choice of theme until a request is processed:

class HomeController < ApplicationController
  theme :theme_resolver

  def index
    ...
  end

  private

    def theme_resolver
      params[:theme].presence || 'professional'
    end
end

Now, if there is a params[:theme], it will use that theme. Otherwise, it will use professional theme.

You can even use an inline method, such as a Proc, to determine the theme. For example, if you pass a Proc object, the block you give the Proc will be given the controller instance, so the theme can be determined based on the current request:

class HomeController < ApplicationController
  theme Proc.new { |controller| controller.params[:theme].presence || 'professional' }
end

Theme specified at the controller level support the :only and :except options. These options take either a method name, or an array of method names, corresponding to method names within the controller:

class HomeController < ApplicationController
  theme 'basic', except: [:rss]
end

With this declaration, the basic theme would be used for everything but the rss index methods.

Authors

themes_on_rails's People

Contributors

bastengao avatar chamnap avatar johnwmcarneiro avatar lowjoel avatar stevenharman avatar victorperez avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

themes_on_rails's Issues

inline theme proc got error

i am use rails 4.2.4
my controller code like this

class Sites::ApplicationController < ::ApplicationController
  before_action :set_site
  #theme Proc.new { |controller| @site.theme || 'default' }
  theme Proc.new { |controller| params[:theme].presence || 'professional' }

  private
    def set_site
      @site ||= Site.find(params[:site_id])
    end
end

but got error:

undefined local variable or method `params' for Sites::ApplicationController:Class

Asset Pipeline not working for linked non-theme assets?

If I namespace assets, in production the expected behaviour of asset pipeline adding the fingerprints works:

= stylesheet_link_tag    "castable/all"
becomes
//castable.storage.googleapis.com/assets/castable/all-76d7d8eaea4fbc6345d3f05388ffcf53.css

I'm trying to link to non-namespaced (not in theme folder) assets, however this doesn't get the fingerprint:

= javascript_link_tag "application"
= "//castable.storage.googleapis.com/javascripts/application.js"
= javascript_link_tag "admin/dropzone.js"
= "//castable.storage.googleapis.com/javascripts/admin/dropzone.js"

Is there a way around this?

Unable to clear the browsers cache when using themes

In a normal application(without themes), the below code is used to clear the browsers cache

class ApplicationController < ActionController::Base

before_filter :set_cache_headers

  private

  def set_cache_headers
    response.headers["Cache-Control"] = "no-cache, no-store"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
  end
end

so that to prevent the pages to be viewed by clicking the browsers back button after user signout. But with themes, i'm unable to get that above code working. Please help me out. Thanks.

Publish new release

The i18n.default_locale configuration is also in latest version, bug it is fixed in master. Please publish new release.

Dynamic theme name seems to be having trouble loading the correct template, ActionView::MissingTemplate

I'm getting these errors:

Started GET "/" for 127.0.0.1 at 2014-06-29 17:02:57 -0400
Processing by PostsController#index as HTML
Vanity: loading experiments from C:/Users/Chloe/workspace/Tyger/experiments
Vanity: loading metrics from C:/Users/Chloe/workspace/Tyger/experiments/metrics
*** {"session_id":"9541a5d2f4d6726039092b0ac22c3bae","_csrf_token":"RQB4Ji98sTvfETiZcW7caAYJYe0ytqUMC1QPO3f+2fo=","user_id":43,"theme":0}
*** loading theme bootstrap
Completed 500 Internal Server Error in 20ms

ActionView::MissingTemplate (Missing template layouts/ancap with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :haml]}. Searched in:
  * "C:/Users/Chloe/workspace/Tyger/app/themes/bootstrap/views"
  * "C:/Users/Chloe/workspace/Tyger/app/views"
):
  app/controllers/posts_controller.rb:44:in `block (2 levels) in index'
  app/controllers/posts_controller.rb:43:in `index'

Notice it correctly selects theme bootstrap, but then tries to load a different template layout!

Here is my #theme selector in ApplicationController:

  theme lambda {|controller| 
    puts "*** #{controller.session.to_json}"
    name = ['bootstrap', 'ancap'][controller.session[:theme].to_i] 
    puts "*** loading theme #{name}"
    name
    }

Here is line 43 & 44 of posts_controller if you are curious:

respond_to do |format|
  format.html {render :index}

How to let user switch the themes

Hi,
Thanks for the nice gem, its easy to use ...but can you please let me know how to let user switch the themes using some kind of theme switcher ..it would be great help if you can share some sample code for theme switcher ..also pls let me know if its possible to let user edit css or html from his account..Thanks

Using Themes_on_rails and Liquid together

@chamnap and others, kindly help out, i'm new to ruby on rails, i'm currently working on a project that required user to have a sub-domain under the main website, i'm currently using theme_on_rails to create themes but don't know how i can bring together, the templates (html and CSS) which reside on theme directory and Liquid engine.

i'll appreciate your help.

regards

Missing Templates

Every time I create a controller, I need to transfer files to a folder of the controller to the theme? No function built-in?

Example:

$ rails g themes_on_rails:generatelayouts SomeController

Just does'not exists in doc. about this.Thx.

Gem in an engine

Hi,

i am trying to use this gem inside an egine in my application.
How can i use it as it seems it is always searching inside root app paths, not my engine paths.

Is there any way to tell where it starts searching from?

undefined method `theme' in Controller Class. Rails 5.0.1

Hi.
gem 'themes_on_rails'
is added into Gemfile and installed

Themes are generated:
rails g themes_on_rails:theme work

But, after calling method theme inside a controller:

class Request::CatalogController < ApplicationController

	theme 'work'

	def home
		@items = Item.get_items
	end

end

i get an error:
undefined method 'theme' for Request::CatalogController:Class (NoMethodError)

Rails version 5.0.1

Thank you!

Loading manifest.js for Theme

Rails 6 introduced the manifest.js file that declares where the assets to precompiled live:

//= link_tree ../images
//= link_directory ../stylesheets .css
//= link_tree ../../javascript .js
//= link_tree ../../../vendor/javascript .js

However this means that if I have all.css and all.js inside a theme I get the error:

Asset theme1/all.css was not declared to be precompiled in production.
Declare links to your assets in app/assets/config/manifest.js.

Unless I add:

//= link theme1/all.css
//= link theme1/all.js

To the main manifest.js file... is it possible to get around this?

Templates in themes/ are not cached in production

If using a template engine like Slim, templates in themes will be re-compiled on every single request, because the cache of the compiled template is discarded after every request.

I have a PR coming.

Rails 6 support

Migrating to rails 6

In application controller we have

  theme Proc.new { |controller|
    unify_theme_pahts = ...
    unify_theme_pahts ||= []
    unify_theme_pahts.select { |path| /#{path}/ =~ controller.request.path }.count > 0 ? :theme1 : :theme2
  }

On rails s an error occurs

3: from /home/user/project/app/controllers/application_controller.rb:3:in `<top (required)>'
2: from /home/user/project/app/controllers/application_controller.rb:10:in `<class:ApplicationController>'
1: from /home/user/.rvm/gems/ruby-2.6.5/bundler/gems/themes_on_rails-229eda1371a1/lib/themes_on_rails/controller_additions.rb:9:in `theme'
/home/user/.rvm/gems/ruby-2.6.5/bundler/gems/themes_on_rails-229eda1371a1/lib/themes_on_rails/action_controller.rb:29:in `apply_theme': nil is not a symbol nor a string (TypeError)

NoMethodError: undefined method `theme' for ApplicationController:Class

Thanks for your nice work on this theme gem. It's nice to use. Sadly we hit this bug, adding the theme method to our ApplicationController: NoMethodError: undefined methodtheme' for ApplicationController:Class`.

Adding the method on a different controller makes no problems at all. But since we have for example Devise inheriting from the ApplicationController and we would like to use theming also for the registration etc. views, we need it inside the ApplicationController.

You can find a stacktrace of the exception at this gist. I think it's some dependency problem with Devise, ActiveAdmin or InheritedResources. I tried moving your gem to the top of the Gemfile, but this doesn't changed anything.

Would be great if you could have a look into this.

Thanks for your time and effort!

New gem release 0.3.0

Can you release a new gem?

The 0.2.0 gem throws an error in production in the railtie and is fixed on master.

Let me know if I can help?

Production custom assets precompile

Hi,

i'm struggling with a production precompilation of custom assets.
I'm on Rails 4.2.1

For example, there are "controller" javascript files which need to be precompiled as they are not in application.js, but i can't find a way to do it. Same goes for custom stylesheets.

Any tips?

set locale in ApplicationController

first off, great gem

second, I think it's good to state in the read me that setting a default in application.rb or a custom initializer does not work when using this gem (it sets the default locale to :en)

however you can still set it in the application_controller.rb (as stated in the Rails doc)

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  theme :theme_resolver
  before_action :set_locale

  private
  def theme_resolver
    ENV['APP_THEME'] || Settings.theme
  end

  def set_locale
    I18n.locale = Settings.locale
  end
end
  • Settings is rails_config

Fallbacks to base theme and inheritance

How can we have it so that if no theme is found it falls back to the default app/views?

And using this same mechanism would it be possible to get it to first look in the theme folder, then have a parent theme to fallback to and then finally the default Rails folder if it can't be found.

Sprockets::CircularDependencyError when including all.css

When I try to add a theme, it complains the file is already included.

= stylesheet_link_tag    "ancap/all", media: "all", "data-turbolinks-track" => true

I have to remove the following two lines to get it to work:

 *= require_self
 *= require_tree .

Here is the error message:

Sprockets::CircularDependencyError in Posts#index
Showing C:/Users/Chloe/workspace/Tyger/app/themes/ancap/views/layouts/ancap.haml where line #16 raised:

C:/Users/Chloe/workspace/Tyger/app/themes/ancap/assets/stylesheets/ancap/all.css.scss has already been required

Include default assets in themes

By default, scripts images and styles in assets folder should be loaded...
For example, I have a chart.js in my asset folder, that will be used independent of the theme... I should't need to put a copy on each theme...

Is there a workaround for this?

allow to change themes

will be nice to have a method 'theme_selector' that will help in the development process so you can test without typing the parameter on the url

Is it possible to allow multiple layouts in a theme

I want something like this which uses 'admin.html.erb' layout file in black theme,

class AdminController < ApplicationController
  theme 'black', layout: 'admin'

  def index
    @users = User.all
  end
end

And this will use a default layout "black.html.erb"

class UsersController < ApplicationController
  theme 'black'

  def index
    @users = User.all
  end
end

Can we make it possible?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.