Here's how you can implement a button to generate the identifier and token, with different display logic for the index and show views:
1. Add a Member Action in ActiveAdmin
ActiveAdmin.register CampaignPartner do
# ... existing code ...
member_action :generate_identifier_token, method: :post do
# Generate identifier and token
resource.identifier = resource.name.parameterize(separator: '-')
bearer_token = SecureRandom.hex
hash_token = Rails.application.credentials.secret_key_base
resource.token = OpenSSL::HMAC.hexdigest('SHA256', hash_token, bearer_token)
if resource.save
redirect_to resource_path, notice: 'Identifier and Token generated successfully.'
else
redirect_to resource_path, alert: 'Failed to generate Identifier and Token.'
end
end
end
2. Update Index View
index do
selectable_column
column :id
column :name
column :identifier do |campaign_partner|
if campaign_partner.identifier.present?
campaign_partner.identifier
else
link_to 'Create', generate_identifier_token_admin_v1_campaign_partner_path(campaign_partner),
method: :post,
class: 'button',
data: { confirm: 'Generate Identifier and Token?' }
end
end
column :token do |campaign_partner|
if campaign_partner.token.present?
campaign_partner.token
else
link_to 'Create', generate_identifier_token_admin_v1_campaign_partner_path(campaign_partner),
method: :post,
class: 'button',
data: { confirm: 'Generate Identifier and Token?' }
end
end
column 'Campaign Partner' do |campaign_partner|
campaign_partner.try(:user).try(:email)
end
# ... rest of the existing index code ...
end
3. Update Show View
show do |campaign_partner|
attributes_table do
row :id
row :name
row :identifier do
if campaign_partner.identifier.present?
campaign_partner.identifier
else
link_to 'Generate Identifier and Token',
generate_identifier_token_admin_v1_campaign_partner_path(campaign_partner),
method: :post,
class: 'button',
data: { confirm: 'Generate Identifier and Token?' }
end
end
row :token do
if campaign_partner.token.present?
campaign_partner.token
else
link_to 'Generate Identifier and Token',
generate_identifier_token_admin_v1_campaign_partner_path(campaign_partner),
method: :post,
class: 'button',
data: { confirm: 'Generate Identifier and Token?' }
end
end
# ... rest of the existing show code ...
end
end
4. Optional: Add Permit Params
If you haven't already, make sure to add :identifier and :token to your permitted params:
permit_params :name, :user_id, :partner_type, :identifier, :token,
# ... other existing permitted params ...
5. Add Some CSS (Optional)
You might want to add some CSS to make the button look better. In your app/assets/stylesheets/active_admin.scss:
.button {
background-color: #5cb85c;
color: white;
padding: 5px 10px;
text-decoration: none;
border-radius: 3px;
&:hover {
background-color: #4cae4c;
}
}
Additional Considerations
- Authorization: You might want to add authorization checks to ensure only certain users can generate identifiers and tokens.
member_action :generate_identifier_token, method: :post do
# Add authorization check
authorize! :manage, CampaignPartner
# Rest of the existing code...
end
- Validation: You might want to add some validations to ensure unique identifiers:
class CampaignPartner < ApplicationRecord
validates :identifier, uniqueness: true, allow_nil: true
validates :token, uniqueness: true, allow_nil: true
end
- Regeneration Logic: Decide if you want to allow regeneration of identifier and token or prevent it once they're set.
Example Additional Logic for Regeneration
member_action :generate_identifier_token, method: :post do
# Prevent regeneration if already set
if resource.identifier.present? || resource.token.present?
redirect_to resource_path, alert: 'Identifier and Token are already set and cannot be regenerated.'
return
end
# Existing generation logic...
end
This implementation provides:
- A button to generate identifier and token
- Conditional display based on whether identifier/token exists
- A confirmation dialog before generation
- Flexibility to customize further based on your specific requirements
Remember to adjust the paths (generate_identifier_token_admin_v1_campaign_partner_path) according to your actual route configuration.