ActiveAdmin - Custom action with reusing the show form - activeadmin

I am creating a custom action that serve as show view with ActiveAdmin, is there any helper I can reused on the partial so I can reuse the ActiveAdmin show view template.
I reckons with show action, you could customise the arrangement with attributes_tables, is there something similar I could reuse for the view?
show do |ad|
attributes_table do
row :title
end
end
Here's my custom action:
member_action :read do
#app = App.find(params[:id])
#Rendering Partial
end

You could use partials in both views, with a bit of a hack:
app/admin/apps.rb
ActiveAdmin.register App do
show do
#app = App.find(params[:id])
render "show", context: self
end
member_action :read do
#app = App.find(params[:id])
end
end
app/views/admin/apps/_show.builder
context.instance_eval do
div do
panel "Address" do
attributes_table_for #address do
row :address_line_1
end
end
active_admin_comments_for #address
end
end
app/views/admin/apps/read.builder
render "show", context: self
Edit: Made it work!
Edit 2: Made comments work!

Related

Make Menu Item Do Something AppleScript

I'm writing a program in AppleScript that creates a menu in the menu bar on MacOS. This is my code:
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
property StatusItem : missing value
property selectedMenu : "" -- each menu action will set this to a number, this will determin which IP is shown
property theDisplay : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"
property theList : "Settings Battery Quit"
-- example list for the menu items that can be used. Ideally you will have your list created dynamically
(*MENU ITEMS
- Settings
- Battery Stats
----------
- Quit
*)
-- check we are running in foreground - YOU MUST RUN AS APPLICATION. to be thread safe and not crash
if not (current application's NSThread's isMainThread()) as boolean then
display alert "This script must be run from the main thread." buttons {"Cancel"} as critical
error number -128
end if
on menuNeedsUpdate:(menu)
(* NSMenu's delegates method, when the menu is clicked this is called.
We use it here to call the method makeMenus(). Which removes the old menuItems and builds new ones.
This means the menu items can be changed dynamically.
*)
my makeMenus()
end menuNeedsUpdate:
on makeMenus()
newMenu's removeAllItems() -- remove existing menu items
-----< (* this is just to show in this example a dynamic list for the menu items
set allMenuItems to {"Settings", "Battery Stats", "Quit"}
---- <
repeat with i from 1 to number of items in allMenuItems
set this_item to item i of allMenuItems
set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:"someAction:" keyEquivalent:"")
(newMenu's addItem:thisMenuItem)
(thisMenuItem's setTarget:me) -- required for enabling the menu item
if i is equal to 2 then
(newMenu's addItem:(current application's NSMenuItem's separatorItem)) -- add a seperator
end if
end repeat
end makeMenus
--menuItems action is requied for the menu to be enabled
on someAction:sender
MenuItem --do some thing
end someAction:
-- create an NSStatusBar
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"IP"
-- set up the initial NSMenu of the statusbar
set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
newMenu's setDelegate:me (*
Requied delegation for when the Status bar Menu is clicked the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.
*)
StatusItem's setMenu:newMenu
end makeStatusBar
my makeStatusBar()
The function
on someAction:sender
--MenuItem --do some thing
end someAction:
is what I need to change. I don't know how to run specific tasks based on which menu item is clicked. When I put code straight in the function:
on someAction:sender
--MenuItem --do some thing
display dialog "This runs on menu item click"
end someAction:
then the code runs whenever ANY menu item is clicked. I want it to run specific tasks for specific menu items. The menu items are:
Settings
Battery Stats
Quit
I tried this:
on someAction:sender
--MenuItem --do some thing
if sender is equal to "Settings"
--Code here!
end if
end someAction:
However, nothing happened when I clicked "Settings" in the menu.
Any help would be appreciated!
You can run specific tasks for specific menu items by checking the sender title:
on someAction:sender
set theTitle to title of sender as string
if theTitle is "Settings" then
display dialog "This runs when menu item \"Settings\" clicked"
else if theTitle is "Battery Stats" then
display dialog "This runs when menu item \"Battery Stats\" clicked"
else if theTitle is "Quit" then
quit
else
-- do nothing
end if
end actionHandler:

In Active Admin / Arbre form, how I can put HTML (specifically a line break) in a label?

This is a little unorthodox, but I don't think it should be difficult.
I have a Active Admin form:
form do |f|
f.semantic_errors(*f.object.errors.keys)
f.inputs do
f.input :email
f.input :name
# read-only field that still matches formatting of form
li do
label "Last Update Time<br/>(except password changes)"
div f.object.last_update_time
end
end
f.actions
end
When rendered, the last_update_time label does not convert the <br/> into a line break. Similarly, html entity codes such as © (the copyright (c)) aren't converted either.
How can I get html to render in that label?
Stuff I tried that doesn't work:
label "foo<br/>bar".html_safe
label raw("foo<br/>bar")
a block like this (gets an error on label: "wrong number of arguments (given 0, expected 1..3)"):
label do
"foo"
br
"bar"
end
Anybody know the trick?
Try next:
li do
text_node "<label>Last Update Time<br/>(except password changes)"</label>".html_safe
div f.object.last_update_time
end
© must be inside a text_node too like:
text_node "©".html_safe
Alas Formtastic is not built on Arbre and is somewhat hacked into ActiveAdmin. I would try replacing Formtastic's label here with Arbre's text_node

Icons In ListView

I am trying to add icons next to text in a listview, but I keep getting an error saying I need to imagelist must be initialised before it can be used.
On the form window as it loads, I have the following code:
Private Sub UserForm_Initialize()
ImageList3.ListImages.Add Key:="Koala", Picture:=LoadPicture("\images\add_icon.gif")
end sub
Then I am trying to populate the listview like this
li.ListSubItems.Add , , "Test", "Koala"
What am I doing wrong?
You need to link your ImageList to the ListView using the Icons-property. Add the following line in the UserForm_Initialize - this must be done before you populate the ListView (of course you need to adapt the name of the ListView)
Me.ListView1.Icons = ImageList3

TabBar Segue when selecting row in table view controller

I already have a segue set in place in my table view controller for an add button press. Now I want to have another segue for when the user selects on the newly added object. My code looks like this so far:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"Add Vehicle Segue"])
{
NSLog(#"Setting RootViewController as a delegate of AddViewController");
AddViewController *addViewController = segue.destinationViewController;
addViewController.delegate = self;
addViewController.managedObjectContext = self.managedObjectContext;
}
I think I will need to add an else if next to take care of the tab bar segue but don't know how to do that.
Also the tab bar view is just there to show the two tabs so I don't believe I will preform the segue with the reference to core data.
Thanks in advance.!
My storyboard flow
If you do not want to pass value to the next view controller or do any actions that need to done before performing the segue, but just display the tab bar then you don't need to add anything in the prepareForSegue:sender: method. If you do have to do something when performing the segue add and else condition as there is only two segues and write your code inside the else block.

Dynamically Change Button Image in Excel 2010

I'm using XML to create a custom Excel Ribbon in which I have a button that contains an image. I would like to be able to change this image when the button is pressed (like a toggle) to show what state it's in. I have the following XML to describe the button:
<button id="MyButton"
label="MyLabel"
screentip="Some useful info."
onAction="MyAction"
getImage="GetImage"
size="large"/>
Where the MyAction method is defined as:
public void MyAction(Office.IRibbonControl control)
{
// Change button image here...
}
Is there some way I can change the button's image in the MyAction() method?
Cheers
EDIT: sorry, I just realised this was on the C# forum and I wrote the functions in VB. Do you need me to have the code translated into C#? Note that the callback behavior should be the same.
I find the easiest way to do update a control is to Invalidate the control or the whole ribbon and have the parameters update through the control's callbacks like getImage, getVisible, getLabel, etc
What I write here will work for any of a controls' parameters (label, enabled, visible, ...)
So if you define your button in the XML this way:
<button id="MyButton"
label="MyLabel"
onAction="OnAction"
getImage="GetImage"/>
You can then use the OnAction callback to update the button's paramater this way (assuming that you have a boolean variable called condition):
Public Sub OnAction(ByVal control As Office.IRibbonControl)
// Do your button stuff here
condition = Not condition
gui.InvalidateControl(control.Id)
End Sub
Then the callbacks of the button will get called, for getImage you can use:
Public Function GetImage(ByVal control As Office.IRibbonControl) As String
If condition Then
Return "MacroPlay"
Else
Return "DeclineInvitation"
End If
End Function
Note that for all this to work you need to store the Ribbon into the gui variable. For this you need to use in the XML:
<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" onLoad="OnRibbonLoad"> ...
and in the code:
Public Sub OnRibbonLoad(ByVal ribbon As Office.IRibbonUI)
gui = ribbon
End Sub
It looks as though you are correct. The only way I have found to edit the appearance of custom ribbon elements is to use 'Ribbon (Visual Designer)' and not XML.
For example, if I want to change an image on a toggle button called toggleButton1 I can do the following:
public void toggleButton1_Click(object sender, RibbonControlEventArgs e)
{
toggleButton1.Image = GetImage(toggleButton1.Checked);
}
where GetImage is defined as:
public Bitmap GetImage(bool pressed)
{
return new Bitmap(pressed ? Properties.Resources.img_1 : Properties.Resources.img_2);
}
Cheers
<button id="MyButton" label="MyLabel" onAction="OnAction" getImage="GetImage"/>
The solution above worked for me. Just make sure that you don't use loadImage event on your customUI xml node. Looks like if you use both loadImage and GetImage then addin fails to load.

Resources