How do use the Spree theme for Custom pages I create - spree

Is there a better way of using say the blue spree theme on other non spree pages?
I'm just going through and including the files that I can and adding the HTML I can't get from the includes into a master template. Would be nice if there was a better / easier way.

After some digging I figured it out.
All you have to do is set your controller to inherit from the Spree::StoreController
so go into whatever controller you want to do this for and change this:
class FooController < ApplicationController
def index
end
end
To this:
class FooController < Spree::StoreController
def index
end
end

Related

Separate template for dashboard

I need to use a separate template for dashboard. So that
http://mysite/dashboard loads a completely different template.
So, I have created a template under layouts/dashboard_template.html.erb
My dashboard cotroller
class DashboardController < ApplicationController
def index
render template: 'layouts/dashboard_template.html.erb'
end
end
I have added following routes.rb
get 'dashboard', to: 'dashboard#index'
but, it loads the template inside application.html.erb! Not sure how to fix it. Hope it is clear. Please help.
In your dashboard controller use
class DashboardController < ApplicationController
layout :set_layout, only: [:index]
def index
# do your stuff
end
def set_layout
"layout_file_name" || 'application'
end
end
this will load your layout if present else main application layout

Rails 4/ActiveAdmin: Add link to list all objects from has_many relationship

I am using a cached_counter to keep track of all comments for a user.
My model relationship look like this:
class User < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
comment belongs to user, counter_cache: true
end
and the user table holds the counting variable.
In Activeadmin i have a column :comments_count which shows the amount of comments for each user.
So far so good.
Now i would like to modify it a bit. I would like to add a link which directs to a page where all comments are listed. How can do this?
I am checking the AA [live demo][1] since they do something similar there.
My idea was to create a partial view and link_to it. But i m struggling implementing it and i dont know if this is best practise anyways.
I have this query
Comments.where(:User_id => :id)
but how to i embed it into a column?
Thank for any advice .
You can handle this situation by linking to the admin index page for Comment with a filter pre-set for the given user_id. This requires drastically less code then a custom page, and gives you direct access to scopes, filters, etc.
Here's how:
index do
# make sure you set sortable, so you can click to sort!
column :comments_count, sortable: 'users.comments_count' do |user|
link_to user.comments_count,
admin_comments_path(q: { user_id_eq: user.id })
end
end
Edit:
The ActiveAdmin index controller uses Ransack to handle searching and filtering. Ransack accepts query options in the form of a hash that obeys a sort-of DSL (the user_id_eq bit above is an example). Now if you open any ActiveAdmin index route and start playing around with filters, you'll see those parameters tacked on to the end of the url using the same convention. The ?q=... part gets passed directly to Ransack in the index controller, and that's how your models get filtered. Our code above simply links to the index page with the id filter pre-set. You can add other filters, sort orders, or even scopes as well.

Button Redirect to a controller in Rails Slim template

I have a rails controller page home_controller.rb
class HomeController < ApplicationController
def index
##users = User.all
#loan_applications = LoanApplication.all
end
def button_listing_show
redirect_to lp_banker_assignments_path
end
end
And I have this Rails Slim button code
button Go to Listings onclick="<%home.button_listing_show%>"
What i want is on the onclick events the page redirects to this path.
Can someone provide an answer for me either in Slim or in Rails.
Thanks
button Go to Listings onclick="#{lp_banker_assignments_path}"
You are just generating a path with that function, so why not just interpolate to the path directly?
Docs for variable interpolation

How do I make a method available inside a partial?

I'm a rails beginner learning rails 4 and I'm trying to learn by doing. I'm making a simple blog that I want some simple user authentication on. I'm trying to learn here, so I don't want to implement Devise, etc. I have a header partial that takes care of my site header and I'm trying to put a link to logout that only shows if a user is logged in. I have a simple session controller that has a new action for the signup form, a create action that sets the current user after matching the email and password and sets session[:user_id] = #current_user.id, and a destroy action that nils out the session. In my application controller I have a method like this
def logged_in?
!session[:user_id].nil?
end
In my _header.html.erb partial, I have
<% if logged_in? %>
(My link)
<% end %>
When I load the page it tells me it can't find the "logged_in?" method. Anyone know why? Thanks.
Methods on controllers are by default not exposed to the views (which your partial is part of).
2 solutions:
Create your logged_in? method as a helper method, for example in an AuthenticationHelper. Doing this you cannot access it from controllers, though.
Expose your controller method to the view using helper_method:
class ApplicationController < ActionController::Base
helper_method :logged_in?
def logged_in?
[...]
end
end
Couldn't you just use:
if #current_user

django admin list_filter too long

I have a list_filter with loads of sectors. This list, on the right side of the page, is too long.
Can I use an input select field instead since I can't choose more than one sector?
I have seen this before, screenshots, but I can not find the way to do this.
edit:
I have a custom FilterSpec not a list_filter
You can write your own custom FilterSpec (custom admin list filter).
This feature is not part of the Django code yet; it is planned for version 1.2. You'll need to apply this patch to the Django code: http://code.djangoproject.com/ticket/5833.
There are many examples on stackoverflow on how to do that, e.g: https://stackoverflow.com/a/1294952/342473.
http://lincolnloop.com/blog/2011/jan/11/custom-filters-django-admin/
The long list that you said comes from the default template 'admin/filter.html', in django/contrib/admin/templates/admin/filter.html, of builtin ListFilters.
There are several ways to customize it:
Globally override 'admin/filter.html'. Render select tag instead of ul tag if the count of choices hit certain limit. This affects all list filters in admin. The select tag should have onchange event handler like
<select ... onchange="location.href=this.options[this.selectedIndex].value">
Set template attribute in your specific ListFilter instance, to the name of the customized filter template. The content of the template is like #1. Django 1.4+ is required for this.
Add javascript in the ModelAdmin instance to convert HTML content inside the ul tag to select tag as soon as DOM has been fully loaded.
This is how i resolved it (jQuery):
$('#changelist-filter ul').each(function(){
var maxlength = 10;
if ($(this).children().length > maxlength )
{
var list=$(this),
select=$(document.createElement('select')).insertBefore($(this).hide());
$('>li a', this).each(function(){
console.log($(this).parent().attr('class'));
var target=$(this).attr('target'),
option=$(document.createElement('option'))
.appendTo(select)
.val(this.href)
.attr('selected', $(this).parent().attr('class'))
.html($(this).html())
.click(function(){
if (target==='_blank'){
window.open($(this).val());
}
else{
window.location.href=$(this).val();
}
});
});
list.remove();
}
});