Active Admin member action - activeadmin

I have two models :
Project
has_one :abstract
Abstract
belongs_to :project
After reading the active admin documentation I do this :
member_action :abstracts do
#project = Project.find(params[:id])
#abstract = #project.abstract
end
Then I create an abstracts.html.arb in admin/project and I can access to it by this url
/admin/projects/:id/abstracts
My question is how can I add the form to create/edit/delete/show abstract from here ?

You can use this syntax to render forms (Source):
render active_admin_template('edit.html.arb'), :layout => false
But according code you provided - you should use belongs_to syntax from inherited resources.
Belongs to
Finally, our Projects are going to get some Tasks. Then you create a
TasksController and do:
class TasksController < InheritedResources::Base
belongs_to :project
end
belongs_to accepts several options to be able to configure the
association. For example, if you want urls like
/projects/:project_title/tasks, you can customize how
InheritedResources find your projects:
class TasksController < InheritedResources::Base
belongs_to :project, :finder => :find_by_title!, :param => :project_title
end
Active Admin is based on it, so it should work. Documentation.

Related

MitreID Connect : Retrieve LDAP operational attributes

I'm working on the LDAP overlay of MitreID Connect project and everthing is working greatly:
Authentication
Retrieving attributes from LDAP Directory
The problem I have now, is how to retrieve operational attributes in LDAP directory.
I'm not good with Spring development, but I found some documentation which treat this sub, but I'm not able to make it work.
Here's what I found:
Retrieving operational attributes
Ldap Server maintains many operational attributes internally. Example entryUUID is an operational attribute assigns the Universally Unique Identifier (UUID) to the entry. The createTimestamp, modifyTimestamp are also operational attributes assigned to the entry on create or update. These operational attributes does not belong to an object class and hence they were not returned as part of your search or lookup. You need to explicitly request them by their name in your search or build the custom AttributeMapper implementation with matching attribute names.
Now let’s try to retrieve the entryUUID, first you need to build the search controls like this,
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
controls.setReturningObjFlag(false);
controls.setReturningAttributes(new String[]{"entryUUID"});
Once you have search control then it’s simply calling search method just like retrieving any other attributes.
ldapTemplate.search("baseName", "(objectclass=person)", controls, new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs) throws NamingException {
Attribute attrUuid = attrs.get("entryUUID");
return attrUuid;
}});
Here is another way to do the same using ContextMapper,
ldapTemplate.search("baseName","(objectclass=person)", 1, new String[]{"entryUUID"},
new ContextMapper(){
public Object mapFromContext(Object ctx) {
DirContextAdapter context = (DirContextAdapter)ctx;
return context.getStringAttributes("entryUUID");
}
});
Let’s add the filter based off of operational attributes like below,
OrFilter orFilter = new OrFilter();
orFilter.or(new GreaterThanOrEqualsFilter("createTimestamp", "YYYYMMDDHHMMSSZ"));
orFilter.or(new LessThanOrEqualsFilter("modifyTimestamp", "YYYYMMDDHHMMSSZ"));
Now call the above search with the filter
ldapTemplate.search("baseName", orFilter.encode(), controls, new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs) throws NamingException {
Attribute attrUuid = attrs.get("entryUUID");
return attrUuid;
}});

Rails different Devise Mailer layout for the same action

I have a model User that has multiple referenced "profiles". A user can have several of those profiles, and each of those profiles should induce a specific layout in emails. Let's consider the profiles Admin and Worker
For example, in my Devise confirmation controller, I need different layouts depending on the profile the user have. For example, if the user has
Admin profile : render admin template
Worker profile : render worker template
Both : render another template (hybrid)
Therefore I cannot set a layout for the Mailer/Controller, but I need to set it inside the controller action. Let's suppose I have a helper layout_for_user() that can return the layout name for a given user. How would I use it ? For example with Devise mailer ?
class MyDeviseMailer < Devise::Mailer
def confirmation_instructions(record, token, options={})
# whange layout based on `layout_for_user(record)`
#token = token
devise_mail(record, :confirmation_instructions, opts)
end
end
I had to override the devise_mail method
def confirmation_instructions
...
#layout = find_layout_for_user(user)
devise_mail(user, :confirmation_instructions, opts)
end
# Override to include custom layouts
def devise_mail(record, action, opts={})
initialize_from_record(record)
mail(headers_for(action, opts)) do |format|
format.html { render layout: #layout }
end
end

Orchard TagBuilderExtensions - Getting classes assigned to model

I'm trying to work with the grid.cshtml alternate in my theme and need to check if a certain class has been added to the grid through the layout editor, is there a way to either iterate or check against the classes that are assigned to the model?
You can access the classes through the model by doing this in Grid.cshtml (and probably all other elements):
#{
IEnumerable<string> classes = Model.Classes;
var hasMyClass = classes.Contains("myClass");
}
#if (hasMyClass) {
<h1>This Grid has css class 'myClass'!!</h1>
}

Getting domain variables on layout

What is the best way to pass the model variables to layout in Grails? Specifically, I'm using Spring security plugin which has User class. I also have Contact class that looks like this:
class Contact {
String realname
String company
String mobile
String fix
String email
User user
...
What are the options for getting the currently logged in person's company in my layout (main.gsp)?
To add to the above answer, you could alternatively set a session variable for the user when you login in whatever controller method gets called.
You can also just set a session variable for the company in the controller method:
session.company = Contact.findByUser(session.user)?.company
or from the example above
session.company = Contact.findByUser(SecurityContextHolder.context.authentication?.principal)?.company
And in your main.gsp, something like:
<span id="companyName">${session.company}</span>
Do you mean that you need to pass this model for every page, automatically, instead of manual passing it at render at each of controllers? You can use filters there:
def filters = {
all(controller: '*', action: '*') {
before = {
request.setAttribute('loggedInPerson', SecurityContextHolder.context.authentication?.principal)
//Notice, that there is used original Authentication, from Spring Security
//If you need you can load your Contact object there, or something
}
after = {
}
afterView = {
}
}
}
and use loggedInPerson at your gsp:
Hello ${loggedInPerson.username}!
Btw, there is also Spring Security tags, that can help you without using your own filter, like:
Hello <sec:loggedInUserInfo field="username"/>!
If you want to add a certain object to the model, you can also use the "interceptors" provided by grails. To add a certain variable to a particular controller, you can use something like this.
def afterInterceptor = {model, modelAndView->
model.loggedInUser = getLoggedInUser() // retrieve your user details here
}
And you can retrieve loggedInUser in the main.gsp layout as ${loggedInUser}.
If you need to get these details in multiple controllers, you can create a BaseController and keep the afterInterceptor in this BaseController. All controllers which need the reference to the logged in user in their corresponding views should extend the BaseController.

Kohana ORM Relationships

I have a couple of tables and have defined relationships for them.
{Table Department} {Table Unit} {Table Branch}
A Department can have more than one branch, a branch can only belong to one department. I need to be able to get the department name, departmentid, branchname
Branch has an instance of departmentid in it.
How do I pull this in one ORM call?
class Model_Admin_Departments extends ORM
{
protected $_has_many = array('branches' => array ());
class Model_Admin_Branches extends ORM
{
protected $_belongs_to = array('departments ' => array());
I have also created the foreign key constraints on the db side with action cascade on delete. Could this cause problems or that is fine?
Assuming you have the right relationships declared you should be able to use the with(...) method on your ORM object.

Resources