Set Sass variable from controller action in Rails 4 - ruby-on-rails

I made a file called template.css.scss in my Rails app that holds all the color styling that I want dynamically changed based on a user's preferences.
Here's a little sample of it:
/* pink */
$lighter-shade: #F75F9E;
$darker-shade: #B54573;
.navbar-inverse {
background-color: $lighter-shade;
}
#command-header {
color: $darker-shade;
}
I thought I could do a migration to add a column to track the user's styling preference. Of course, there would be a default setting. Then I could update the Sass variables based on the preference.
Unfortunately, from my research it seems like the way to go is to create separate templates and then set those dynamically based on the user's preference. I really hope that's not the case because the way I have things structured right now is concise and efficient, and I'd hate to lose that.
How would you go about this?

one way to handle: get the user's preference and put it into a
data-user-preference="<%= current_user.pref %>">
then use jquery to add the appropriate class based on that pref
$('.class-name').data('user-preference').addClass('whatever')

Related

How to navigate?

Say I create an HTML file with two .page on it. In the first .page, I'd like to have a link to the second .page.
Is there a way to navigate between pages without having to write my own JS? This seems to suggest I do have to write JS: http://view.jquerymobile.com/1.3.2/dist/demos/widgets/navigation/.
However, I'd would rather set an id attribute for one of the pages, then maybe define some data attribute in the link to tell jQuery mobile where to go. Possible?
I'd also like to specify what kind of transition effect to use.
You can use standard anchor links, just give an id to your page and set the transition via the data attribute
Link to Page 2

Editable select/combobox

is there any way (or plugin) to display editable combobox? I have a set of options, but I would like to give possibility to enter custom value.
I've searched the documentation, and I can't find way to do this. I made workaround with javascript, but I'm looking for more elegant solution.
I'm pretty sure that there simply is no HTML form element that does this, and so Rails can't provide you with a helper. As you said, you can work with JS to create something similar (and there should be JS libraries/plugins already out there), or you could just use a select element and add a text field next to it for new values.
HTML5 specification doesn't define such an element. So you may either continue using JS, either try to use autocomplete feature of an input element (although it is not exactly what you want and doesn't compatible with old browsers).

Adding * to required form labels

Does anybody have a nice way of adding * to required form labels, without having to turn to tools like SimpleForm?
I did not like the way SimpleForm added all these weird wrappers and classes to my stuff. I thought the point of SimpleForm was to allow you to write simple, semantic form ERB (which it most certainly does) - but not at the same time mess up your existing layout by adding wrappers and classes at random. I always style my forms before I bring them to Rails, so I like to tell it what classes to use, not the other way around.
Can't you just simply style your labels?
Your label:
<label class="required">MyRequiredField</label>
Your css.
label.required:after{content:"*"}
Or am I missing what you're trying to accomplish?
If you don't like their solution you can see how they implemented and roll your own:
https://github.com/plataformatec/simple_form/blob/master/lib/simple_form/helpers/required.rb

Add style switcher to Rails App admin section?

I have been working on a Rails template recently called BaseApp2. You can see it on GitHub here: http://github.com/dannyweb/BaseApp2
There is an administrators section setup which allows the management of Users and I am working on the ability to add announcements to the public site via the admin section.
What I would really like to do, is come up with two or three alternative colour schemes for the interface and allow the administrator to choose which they prefer and select it. The chosen colour scheme would then show for all users from then on.
I have literally no idea how to go about doing this - ideally I am looking for a tutorial of some sort or something equally beginner-esque.
Thanks in advance!
Thanks,
Danny
It should be fairly easy and there are many ways to do it. You will need to save that preference somewhere. Maybe add an attribute to your user/person model that will specify that preference. And in your admin layout template, based on that preference, add an additional stylesheet. Or, add a class to the body tag and in your style sheet subclass the styles:
body {
background-color: white;
}
body.sunshine {
background-color: yellow;
}
The layout template:
<%= stylesheet_link_tag(current_user.theme) if current_user.theme %>
or
<body class="<%= current_user.theme || '' %>">

Fallback format in rails

for my CMs i want to be able to easily add new themes, my idea was to simply add a mime type for the new theme (so application.theme1.erb would work).
but for 99% of the themes im not going to want to change the views, well not all of them.
is there someway to have rails fall back on html if the themed view isnt present?
I'm pretty new to Rails, so this might not be a perfect answer:
you might want to try using querystring parameters as part of the route like described here:
http://guides.rubyonrails.org/routing.html#querystring-parameters
so eventually something like this would work
map.connect ':theme/:controller/:action/:id'
As I understand it, the theme would be available as params[:theme] in the controller. If no theme is specified you probably have to add another route like
map.connect '/:controller/:action/:id'
to handle that case.
In the i18n guide something similar is described for locales: http://guides.rubyonrails.org/i18n.html#setting-the-locale-from-the-url-params
Hope that helps.
It depends on how much of the layout you want to change with the themes.
If you build your HTML right, most of the things can be done through css. (changing fonts, colours, where the stuff show up)
Then it is quite easy to add just a theme parameter to style it.
If you don't want to do that, you can always create a separate layout for it, and assign that depending on the parameters passed in (or even set it as a session variable so you won't have it in the url).
Basically, for the default theme, you stick to the layouts/application.erb, then you have say layouts/theme1.erb which you then assign with a method
class ApplicationController
layout :decide_layout
def decide_layout
#session[:layout] || 'application'
end
end
Customizing the views would be possible just by doing something like this in your actions:
def my_action
render "my_action_#{#session[:layout]}" if #session[:layout]
end
if #session[:layout] is not set, it will render the default, otherwise it will render your themed view.
Of course, if you set the layout depending on user input, make sure to sanitize and check the layout parameter before.
I've just had this same problem with mobile_fu, which sets the format to :mobile for mobile requests.
It turns out that if an :action.:format.erb template isn't available, Rails will serve :action.rhtml as a replacement in any format.
I can't say whether this will work for layouts, but it certainly works for actions

Resources