ActiveAdmin 'show' layout not same as edit / new - activeadmin

I have customized the form for new/edit to use two column layout for the form fields.
The customization uses formtastic.
Shouldn't the same layout display for "show" by default? Instead I get the activeadmin default one field per row display.
How do I get to display the same layout in "show" as my form (new/edit) pages?

By default the show page has its own layout, so you have to use some css to arrange the inputs, or you can override this method:
https://github.com/activeadmin/activeadmin/blob/master/lib/active_admin/views/components/attributes_table.rb#L22-L43
#collection.each_slice(2) do |records|
td do
content_for(record[0], block || title)
end
td do
content_for(record[1], block || title)
end
end
Or create a new one, and use this in the show block:
module ActiveAdmin
module Views
class AttributesTable < ActiveAdmin::Component
builder_method :attributes_table_for
def row_with_two_fields(*args, &block)
title = args[0]
options = args.extract_options!
classes = [:row]
if options[:class]
classes << options[:class]
elsif title.present?
classes << "row-#{title.to_s.parameterize('_')}"
end
options[:class] = classes.join(' ')
#table << tr(options) do
th do
header_content_for(title)
end
#collection.each_slice(2) do |records|
td do
content_for(record[0], block || title)
end
td do
content_for(record[1], block || title)
end
end
end
end
end
end
Or something similar... I haven't tried it.

I ended up doing the CSS hack
show do
panel "Mandatory Details" do
attributes_table_for contract do
row :account, :class => "column1"
row :customer, :class => "column2"
end
end
panel "Dates And Financials" do
attributes_table_for contract do
row :start_date, :class => "column1"
row :current_end_date, :class => "column2"
row :end_date_w_options, :class => "column1"
end
end

Related

Reference item in group ( Textbox )

I was using below line of code to edit word document's textbox. It is working pretty well.
wdDoc.Shapes("T1").TextFrame.TextRange.Text = sh.Range("Q2"). Value
But now I made some changes in my Word document (template file).
I Grouped few items with Shapes("T1").So now Shapes("T1") is inside Group(3).
Now code give error, because it can't find Shapes("T1").
How can I reference Shapes("T1") within Group (3)?
Thank you.
Only property I have found is Shape.GroupItems
Here is the solution:
With wdDoc.Shapes("Group 3")
For x = 1 To .GroupItems.Count
If .GroupItems(x).Name = "T1" Then
With .GroupItems(x)
.TextFrame.TextRange.Text = sh.Range("Q2").Value
End With
End If
Next
End With

Excel VBA using Selenium - click on list element to change focus

I'm trying to scape data from a website via Excel VBA. I have a web page which has different data depending on a button selection, but the button sits withing a ul list. I can find the element by class using:
.FindElementByClass("shared-filter-button-list_navItem__ZiG2J")
But I can seem to work out how to switch the focus between 'This season' and 'All time' to change to displayed data on the page. Any ideas would be gratefully received. The html is:
<ul class="shared-filter-button-list_navContainer__3hJmS"><li class="shared-filter-button-list_navItem__ZiG2J is-active"><button class="tag-button_btn__1B2dI tag-button__purple__3SyTF shared-filter-button_wrap__3OgbA is-active" value="This season" type="button">This season</button></li><li class="shared-filter-button-list_navItem__ZiG2J"><button class="tag-button_btn__1B2dI tag-button__purple__3SyTF shared-filter-button_wrap__3OgbA " value="All time" type="button">All time</button></li></ul>
It would help to see the page, but if you just want to click the "this season" or "all time" button, just find the buttons inside the list you already have and click one?
update
I misread the provided HTML (its all in one line) and thought that shared-filter-button-list_navItem__ZiG2J was the container ul not the list items, and also that selenium uses 1-based indexes not 0-based.
The code below finds all buttons that match the query and prints their index and text to the debug window.
Private Driver As Selenium.ChromeDriver
Sub Main()
Set Driver = New Selenium.ChromeDriver
Driver.Get "https://www.euroleaguebasketball.net/eurocup/players/lukas-meisner/011187/"
Dim List As Selenium.WebElements
' get a list of button that are children of li with specified class name
Set List = Driver.FindElementsByXPath("//li[contains(#class, 'shared-filter-button-list_navItem__ZiG2J')]/button", 0, 5000)
If List Is Nothing Then
Debug.Print "failed to get list"
Exit Sub
End If
For Index = 1 To List.Count
Debug.Print Index & " -> " & List(Index).Text
Next Index
End Sub
Expected result from this code is:
1 -> This season
2 -> All time
3 -> Regular Season
If you wanted to click the This season button
List(1).Click

Saved Searches Conditional HTML Formatting

I'm trying to highlight/indicate individual fields within a saved search column based on a certain criteria. I don't want to highlight the specific row as that will highlight the entire row and not just the single column.
The rudimentary code I have now is
CASE WHEN {custbody487} = 'On Time'
THEN {custbody487}
ELSE CASE WHEN {custbody487} = 'On Hold'
THEN {custbody487}
ELSE CASE WHEN {custbody487} = 'Late'
THEN {custbody487}
END END END
How do I conditionally highlight only a specific column? I want to highlight On Time as green, On Hold as blue, etc. But I only want to highlight the text in that specific column.
You can add HTML formatting to saved searches.
Make sure you're using Formula(text) under results.
Here's what the above code looks like highlighted.
CASE WHEN {custbody487} = 'On Time'
THEN '<span style="color:green;font-weight:bold">' || {custbody487} || '</span>'
ELSE CASE WHEN {custbody487} = 'Late'
THEN '<span style="color:red;font-weight:bold">' || {custbody487} || '</span>'
ELSE CASE WHEN {custbody487} = 'On Hold'
THEN '<span style="color:blue;font-weight:bold">' || {custbody487} || '</span>'
END END END
Be advised that searches saved this way will not keep their HTML formatting upon export to PDF or CSV. It will be formatted as plain text.

Formatting Excel cell from number to text in rails

I have made an application on which I have provide the feature to import the records from CSV and Excel file. I am using roo gem for it. The record added successfully but the problem is at the time of importing records from excel, it adds .0 to every field which is number. I don't want it because i have some fields like enrollment_no, roll_no, contact_no and it adds .0 to every filed like it made 23 to 23.0. I already had converted these filed to varchar in database and now i want to format the excel cell from number to text. It will solve my problem. Tell me how i will format the excel cell from number to string using rails.
Here is my code for importing the file:
student.rb :
def self.import(file, current_organization_id)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
record = Student.find_by(:organization_id => current_organization_id,:enrollment_no => row["enrollment_no"]) || new
record.organization_id= current_organization_id
record.attributes = row.to_hash.slice(*row.to_hash.keys)
record.save!
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::CSV.new(file.path)
when ".xls" then Roo::Excel.new(file.path)
when ".xlsx" then Roo::Excelx.new(file.path)
else raise "Unknown file type: #{file.original_filename}"
end
end
students_controller.rb :
def import
Student.import(params[:file], session[:current_organization_id])
#puts #session[:current_organization_id].inspect
redirect_to students_path, notice: "Record imported Successfully."
end
new.html.erb :
<%= form_tag import_students_path, multipart: true do %>
<%= file_field_tag :file , :required=> true%> <br/>
<%= submit_tag "Import" , :class => "btn btn-primary btn-block" %>
<% end %>
I am doing something similar in my application but the import is made easier by importing only from csv.
It seems that cell type is a pretty common problem in Roo and there are few workaround suggested using regex or char to include in your cell.
My solution it would be much easier:
# student.rb
COLUMNS_TO_STRING = ["organization_id", "enrollment_no", "contact_no"] # and so on
def self.import(file, current_organization_id)
spreadsheet = open_spreadsheet(file)
header = spreadsheet.row(1)
(2..spreadsheet.last_row).each do |i|
row = Hash[[header, spreadsheet.row(i)].transpose]
row = clean_for row, COLUMNS_TO_STRING
record = Student.find_by(:organization_id => current_organization_id,:enrollment_no => row["enrollment_no"]) || new
record.organization_id= current_organization_id
record.attributes = row.to_hash.slice(*row.to_hash.keys)
record.save!
end
end
def self.clean_for row_as_hash, string_columns_array
row_as_hash.each do |key, value|
if string_columns_array.include?key
row_as_hash[key] = value.to_i.to_s
end
end
end
def self.open_spreadsheet(file)
case File.extname(file.original_filename)
when ".csv" then Roo::CSV.new(file.path)
when ".xls" then Roo::Excel.new(file.path)
when ".xlsx" then Roo::Excelx.new(file.path)
else raise "Unknown file type: #{file.original_filename}"
end
end
get the index of the columns you want to format differently
convert the value imported from float to integer
convert the integer to string

How to use modules to split very long code in active admin?

Activeadmin registers a page on a single file, in which it has all the logic: Index, Show, Edit, etc.
I would like to split, let's say, task.rb into task_index.rb, task_show.rb, task_edit.rb, etc.
So, how should you do that?
NOTE: I know that making an ActiveAdmin.register block in each file (it appends if Task exists) will do the work, but this question aims for a general approach rather than solving this specific inquiry.
-- admin/task.rb
#encoding: utf-8
ActiveAdmin.register Task do
[Lot's of actions]
member_action....
member_action....
member_action....
batch_action....
[Index stuff]
filter....
scope....
scope....
scope....
index do
column...
column...
column...
column...
end
[Edit stuff]
form do |f|
f.input....
f.input....
f.input....
f.input....
f.input....
end
[etc etc etc]
end
----------------
I'm thinking of modules, but I can't figure out how to.
That is how I do this
module source
module ResourceDSL
module ActsAsClone
def acts_as_clone
controller do
def new
instance_variable_name = active_admin_config.resource_class.to_s.underscore
resource = active_admin_config.resource_class.find(params[:id]) rescue nil
attrs = resource.nil? ? {} : resource.attributes
resource = active_admin_config.resource_class.new(attrs)
instance_variable_set("##{instance_variable_name}", resource)
end
end
action_item :only => [:show, :edit] do
if can? :create, resource and (!resource.respond_to?(:live?) or resource.live?)
link_to "Copy", :action => :new, :id => resource.id
end
end
end
end
end
including to ActiveAdmin::ResourceDSL
ActiveAdmin::ResourceDSL.send :include, ResourceDSL::ActsAsClone
And then you can
ActiveAdmin.register Account do
menu :parent => "Billing", :priority => 10
acts_as_clone
end

Resources