How to dynamically transmit params in link_to ? - ruby

I need to use part of my application in iframe on another page. So I need to remove layout on these pages.
I added to ApplicationController
layout :current_layout
def current_layout
if params[:layout] == false.to_s
false
else
'main'
end
end
This works until user click on any link in navigation. So I need to add to every link_to in this part of application something like:
link_to 'store', store_path(params.merge({layout: params[:layout]})
I am wondering that maybe I can refactor that and maybe remove this params.merge from every link?

You could try something like this:
def my_path_helper(string, path_type, params)
link_to(string, polymorphic_path([path_type], params.merge({layout: params[:layout]})))
end
See the docs for polymorphic path here and also this answer that provides an example.
Is this what you were looking for?

Related

How to set humanized name of padrino module?

How are you.
I have just installed padrino framework admin panel.
And it shows several tabs in admin module.
I want to rename the labels. but how to do it?
This is view side code
<%= link_to project_module.human_name, url(project_module.path) %>
Here I can't set human_name of project_module.
And this is module definition in application.rb
access_control.roles_for :admin do |role|
role.project_module :accounts, '/accounts'
role.project_module :venues, '/venues'
role.project_module :shows, '/shows'
end
Now tabs labels are Accounts, Venues, Shows.
how to set them as Users, MyVenues, MyShows?
Thanks
Just seemed to hit this issue tonight as well. It seems that this is a slight bug that is in the core Padrino framework.
The standard navigation logic out of the box will never produce a proper localized text. It renders the output of function .humanize
https://github.com/padrino/padrino-framework/blob/8bd3796d45eae5e3f7dc52316c8c25c44563f8cd/padrino-admin/lib/padrino-admin/access_control.rb#L176
At best, the humanize function will upcase your text. See https://apidock.com/rails/String/humanize
You can replace the human_name reference with the I18n.t() localizable function:
%ul.nav.navbar-nav.pull-left
- project_modules.each do |project_module|
%li{:class => "navbar-module #{('active' if request.path_info =~ /^#{project_module.path}/)}"}
=link_to I18n.t(project_module.name), url(project_module.path)
See http://padrinorb.com/guides/features/localization/ for reference.
One note, the navigation is not model aware, so your translated text will need to be right under the locale reference in the yml file.
Ex.
en:
accounts: Users
venues: MyVenue
shows: MyShows
This should do the trick. Let me know if you have any questions.

Sinatra - Is a sitewide param possible?

I am building a Sinatra app with haml templates and was wondering if it is possible to implement a sitewide param of some sort. The idea, in my case, would be to allow for a different layout.haml to be able to be selected for every route without having to duplicate/rewrite every route in the app.
For example, I was wondering if it would be possible to be able to GET http://domain.com/route/:normal-params/?layout=layout_b, and be able to append ?template=template_choice to any route in the app and use the appropriate layout.
The only solution I can think of, which seems very inefficient, is to duplicate every single route to look for this parameter. I also feel like it could be achieved somehow with a Filter but am unsure how such a thing could be implemented.
You can specify which layout you want to load in your call to haml:
haml :post, :layout => params[:layout].to_sym
That way you can call http://domain.com/route/foo/bar?layout=layout_b and Sinatra will look for the appropriate layout named layout_b to render in.
You'll probably want to specify a default layout to render if none is provided as a URL parameter:
haml :post, :layout => (params[:layout] || "default").to_sym

How to signout a user if the user is not active since last 15 minutes devise activeadmin

I have to logout a user(admin_user) if the user(admin_user) is not using the application for 15 minutes. How it can be done? I have tried installing activeadmin in my vendor and overriding it(putting devise.rb inside initializers folder inside vendor and write set timeout it did not work), but I don't know how that works. Please help me. I cannot write it in devise.rb as it is done via normal login(user).
Also I have one more query, how the main navigation bar can be changed in activeadmin? I need two menu one in main navigation bar and according sub navigation. Is this achievable through activeadmin?
Jut use config.timeout_in = 15.minutes in your devise.rb initializer.
Active admin has nothing to do with that.
I don't know whether this is the correct way of getting layouts, but I found the solution creating my_navigation resource in active admin.
class MyNavigation < ActiveAdmin::Component
def build(namespace, menu)
if current_admin_user
render :partial => "/layouts/admin_header"
else
render :partial => "/layouts/company_admin_header"
end
end
end

Clicking only on links within a specific menu?

I am trying to scrape a page, and need to click on some links within a menu. If I use the search method, I am then stuck with a Nokogiri object, and therefore can not use the click method.
agent.page.search('.right-menu').links_with(href: /^\/blabla\//).each do |link|
region = link.click
end
The following would tell me that links_with is not defined. How can I make a select links from a specific menu? Is there a way I can parse the object back to a Mechanize object?
You can try something like this:
agent.page.search('.youarehere > a').each do |a|
link = Mechanize::Page::Link.new(a.attr('href'), agent, agent.page)
region = link.click
end
not the cleanest way to do it I guess, but Mechanize is doing almost the same in its source code: http://mechanize.rubyforge.org/Mechanize/Page.html#method-i-links
Would be a nice addition though, instead of going through this.

return to default layout behavior using a Proc

I'm adding unobtrusive ajax to my site, so I need to set the layout depending on the request type :
# app/controllers/application_controller.rb
layout Proc.new { |controller| controller.request.xhr? ? 'ajax' : 'application' }
# app/views/layout/ajax.html.erb
<%= render :partial => "shared/flash", :object => flash %>
<%= yield %>
But I also want to use nested layout and have my javascript deal with where to insert ajax retrieved content. According to the rails api layout nil force default layout behavior with inheritance, so I tried this :
layout Proc.new { |controller| controller.request.xhr? ? 'ajax' : nil }
Which doesn't work, I get no layout at all. The only piece of information I found is an old chat log saying that this, indeed, doesn't work due to the way Rails handle layout.
Is there a way to achieve this ?
Since a proc returning nil doesn't work, is their a way to set the layout on a condition beeing met (request.xhr? here). Something like layout_if.
Or should I just add
layout Proc.new { |controller| controller.request.xhr? ? 'ajax' : 'controller_name' }
to every controllers with a different layout ?
My ajax request actually return html, should I cheat a little and return those html elements in .xml files and use respond_to to set the layout ?
(This may be great, for another reason, having differents urls to access with and without layout content could allow for page cache, but there are other way to achieve this.)
Or maybe there is a way better solution.
Rails can do this automatically. For example you create 2 layouts(application.html.erb and application.js.erb). When request is xhr? it will use js(application.js.erb) layout, if standard request comes to the server rails will render application.html.erb.

Resources