how to use a controller variable in my layout - layout

I am trying to use a variable of my controller in my layout.
For example:
#posts = Post.all.count
In my layout I want to list the Post count, even when I open the index view of another controller.
Many thanks!!!

Two solutions:
Use <%= Post.all.count %> in your layout.
Add a before_filter in your ApplicationController that loads the variable.
class ApplicationController < ActionController::Base
before_filter :load_layout_variables
protected
def load_layout_variables
#posts = Post.all.count
end
end

Related

Change class for a core shape

I'm trying to change class for Pager from pager to pagination as it's conflicting with bootstrap classes.
It's defined in CoreShapes.cs with
Shape.Classes.Add("pager");
I managed to add classes I need by overriding Pager.cshtml
#{
// number of page number links to show, 0 means no link, 1 means only the current page, or more accepted.
Model.Quantity = 5;
Model.Metadata.Alternates.Clear();
Model.Classes.Add("pagination pagination-sm");
Model.Metadata.Type = "Pager_Links";
}
#Display(Model)
This results in list that has class="pager pagination pagination-sm".
Is it possible for me to remove the pager class without modifying CoreShapes.cs or using javascript? Already tried without success:
Model.Classes.Clear();

How can I know an object is not visible when any of parent object is not visible in an HTML page using java script?

Let say I have an text box in an HTML page as follows.
<DIV style = "display:none;">
<DIV style = "display:inline;">
<INPUT type = "text" style = "display:inline;">
</DIV>
</DIV>
In this case, the text box will not be visible to the user. How can I identify that text is not currently visible to the user.
Dont say that, I should travel up to the parent objects to find out if they are set to not visible. I have bunch of fields to be validated like this and this would reduce the application performance.
Is there any other way to find out as this object is not visible to the user?
Thanks in advance.
If you don't need it to be pure JavaScript I would suggest using jQuery. Using the :visible or :hidden selector will accomplish what you want:
if ( $('yourElement').is(":hidden") ) {
// The element is not visible
}
http://api.jquery.com/visible-selector/
http://api.jquery.com/hidden-selector/
If you need pure JavaScript and you don't want to travel up through every ancestor element, you could try checking the element's offsetWidth and offsetHeight. If the element is hidden because of an ancestor element, they should both be 0. Note: I've always used jQuery for this, so I don't know how reliable this is.
var yourElement = document.getElementById('yourElementsId');
if ( yourElement.offsetWidth == 0 && yourElement.offsetHeight == 0) {
// The element is not visible
}
https://developer.mozilla.org/en-US/docs/DOM/element.offsetWidth
https://developer.mozilla.org/en-US/docs/DOM/element.offsetHeight

How do I find all elements which have a css class in coded ui?

I need to find all Html controls which have a given css class.
var htmlControl = new HtmlControl(document);
htmlControl.SearchProperties[HtmlControl.PropertyNames.Class] = #class;
var uiTestControlCollection = htmlControl.FindMatchingControls();
Using the class name works when there is just one css class on the control. If I have more than one css classes applied on the element, can I search for the element by specifying just one css class and not all of them?
Thanks
You can perform a partial match, like so:
htmlControl.SearchProperties.Add(HtmlControl.PropertyNames.Class, #class, PropertyExpressionOperator.Contains);
var uiTestControlCollection = htmlControl.FindMatchingControls();
The main draw back of this is that it is just a simple string compare. To illustrate, imagine you have two controls A and B. A has class "Test" and B has classes "testdiv topnav". Now if you perform a search for "test", both controls A and B will be selected.
To match a class exactly, you can provide a close as match as possible using the above method and write a helper function to:
Loop through the collection
Get the class of each control
Split the class string on the spaces
Loop through this array and test each for an exact match
Keep the elements where a class matches exactly
Note: This is clearly non-optimal - I'm all ears if someone has a better solution.
Cheers,
Seb

Properly rendering multiple layouts per controller in Rails

I've defined in my Users_controller:
layout "intro", only: [:new, :create]
Here's what my layout looks like:
Intro.html.haml
!!! 5
%html{lang:"en"}
%head
%title Intro
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body{style:"margin: 0"}
%header
= yield
%footer= debug(params)
When I render a page that calls for intro as the layout, it gets nested inside my application.html.haml file which is not good.
Is there some way of avoiding this undesirable nesting of layouts?
Thanks in advance!
The problem was in my Controller. I was declaring multiple layout instances like so:
class UsersController < ApplicationController
layout "intro", only: [:new, :create]
layout "full_page", only: [:show]
...
end
Don't do this! The second declaration will take precedence and you won't get your desired affect.
Instead, if your layouts are simply action-specific, just declare it within the action like this:
def show
...
render layout: "full_page"
end
Or, if it's a bit more complex, you can use a symbol to defer the processing to a method at runtime like this:
class UsersController < ApplicationController
layout :determine_layout
...
private
def determine_layout
#current_user.admin? ? "admin" : "normal"
end
end

Rails3 get current layout name inside view

I have the answer for the Rails 2.X but not for Rails 3. How can I read the name of a current layout rendered inside a view.
My Rails2 question: Rails Layout name inside view
Thx.
Getting this to work in Rails 3.2 is a little more complicated than previously outlined. If your controller explicitly declares a layout, then the result of controller.send(:_layout) is a String, but otherwise it's an ActionView::Template. Try this:
module ApplicationHelper
def current_layout
layout = controller.send(:_layout)
if layout.instance_of? String
layout
else
File.basename(layout.identifier).split('.').first
end
end
end
in rails 5
This works for me:
def current_layout
layout = controller.class.send(:_layout)
if layout.nil?
default_layout
elsif layout.instance_of? String or layout.instance_of? Symbol
layout
else
File.basename(layout.identifier).split('.').first
end
end
For Rails 4:
controller.send(:_layout)
=> 'application'
For Rails 3.2:
controller.send(:_layout)
=> #<ActionView::Template:0x000000082bb788>
But controller.send(:_layout).identifier returns the fullpath:
/home/davidm/Documentos/Devel/myapp/app/views/layouts/application.haml
I think it should be in core, but for now you can make a helper method:
def current_layout
controller.send :_layout
end
it will return currently used layout name
I have used in Rails4 at view pages and got reuslt.
controller.send(:_layout)
I hope this help.
For rails 5:
controller.class.send(:_layout)
This does NOT work:
controller.send(:_layout)
You can do what I've done in my Ajax gem for Rails which is to wrap the _render_layout method:
ActionView::Base.class_eval do
def _render_layout_with_tracking(layout, locals, &block)
controller.instance_variable_set(:#_rendered_layout, layout)
_render_layout_without_tracking(layout, locals, &block)
end
alias_method_chain :_render_layout, :tracking
end
Then you can access the value that was set from your view (I'm pretty sure you have access to the controller there...) or in your controller in an after_filter, which is what I do.
I've written a custom RSpec 2 matcher which can be used to test layout rendering in Rails 3.
All the approaches in the previous answers try to guess the name via private methods, but there's no need to guess and can be easily accomplished with the public API:
class ApplicationController
layout :set_layout
attr_reader :layout_name
helper_method :layout_name
private
def set_layout
#layout_name = "application"
end
end
Override in any controller that won't use the standard layout:
class MyController < ApplicationController
private
def set_layout
#layout_name = "my_layout"
end
end
And now in your views:
<%= layout_name %>

Resources