Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fallbacks to base theme and inheritance #40

Open
iamdriz opened this issue Oct 23, 2017 · 9 comments
Open

Fallbacks to base theme and inheritance #40

iamdriz opened this issue Oct 23, 2017 · 9 comments

Comments

@iamdriz
Copy link

iamdriz commented Oct 23, 2017

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.

@yogeshjain999
Copy link

As given in the README, you could either define method or Proc for this in the controller.

theme :default_theme

private

def default_theme
  theme || parent_theme || "layout_name"
end

@iamdriz
Copy link
Author

iamdriz commented Oct 24, 2017

@yogeshjain999 So you pass the default layout as the fallback theme?

@yogeshjain999
Copy link

Correct @iamdriz. It is because search locations includes app/themes and app/views Dir.

@iamdriz
Copy link
Author

iamdriz commented Oct 24, 2017

@yogeshjain999 That being said though.... If I have:

theme :theme_resolver

private

def theme_resolver
  'a_theme_that_doesnt_exist' || 'a_theme_that_does_exist' || 'application'
end

I'm finding that instead of it falling back to the 'a_theme_that_does_exist' it falls straight back to the default views. Probably because I have a value for the first theme (even though it doesn't exist) in the operator and it will try and find that and then just fallback... what I want it to do is fallback on a view-basis to the next operator value and then the default views if those can't be found either. To achieve that, would I need to manually check if the view requested existed first based on the currently requested theme and then try the next one until defaulting to application?

@yogeshjain999
Copy link

That being said, easiest way I can see is doing it like,

def theme_resolver
  theme_present?('a_theme_that_doesnt_exist') || 'application'
end

def theme_present?(name)
  return name if Rails.application.paths["app/themes/#{name}"].present?
end

@iamdriz
Copy link
Author

iamdriz commented Oct 24, 2017

@yogeshjain999 The theme may exist but the view for that particular request may not.

@iamdriz
Copy link
Author

iamdriz commented Oct 24, 2017

In order to get the fallback theme to work I had to do:

before_action :fallback_theme
def fallback_theme
  self.prepend_view_path 'app/themes/fallback_theme/views'
end

@iamdriz
Copy link
Author

iamdriz commented Oct 24, 2017

It also turns out that it doesn't actually use the fallback string:

def theme_resolver
  params[:theme] || 'I CAN PUT ANYTHING HERE'
end

So it would be good to pass back a nil if no theme is specified. Rather than a string that is basically ignored as the default paths are already in the view_paths. Just feels a bit of a hack.

@iamdriz
Copy link
Author

iamdriz commented Oct 24, 2017

After playing around with this I've come to find that building a Rails Engine is probably a better way forward as then it means you can have everything a Rails app would have on a theme basis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants