Conditional display active admin - ruby-on-rails

I have some rules defined in my one of two tables which will define how some fields should be shown
Student Active Admin/model
My first table displays
name age email
alex 21 alex#test.com
I want age column to be displayed red if value is greater than 20
Here is my other modal where I have created rule.
Rules Active Admin Class/Model
name age_limit permission
alex 20 red
How can I apply these permissions in my table
ActiveAdmin.register Student do
index do
column :name
column :age
column :email
actions
end
end
How Can I set conditions in my Student Active Admin Class/Model

Related

Update Attribute in Other Model

I have two tables/models (User and Holiday). A user has a set number of holidays. In the Holiday model, a user can book a number of holidays (integer).
Example:
1) John has a total 10 holiday credit
2) John books 4 holidays
Result: John has 6 holidays left
I know it has something to do with adding a method in User and passing the holidays taken into it.
How do I go about deducting the holidays booked from the holiday credit?
In the in User model:
# assuming, you have a method `holidays_booked`,
# that returns the number of booked holidays
def holidays_left
10 - holidays_booked
end
That's it.
If each user has a holiday property/field, then through associations between the two tables the value can be calculated.
When setting up the database a default value for the holiday credit can be defined. For example:
If you are adding a column to hold the value:
add_column :users, :holiday_credit, :integer, :default => 10
Now every user will have a default holiday credit in your database. This could also be done at object creation time during the object's initialization.
To update the value, a method within the User model can be written like so:
class User < ActiveRecord::Base
def calculate_holiday_credits
self.holiday_credit = default_value - Holiday.find(self.id).count
end
end
The function can be modified to accommodate your needs, but I hope this gives you the general idea.
In short, you want to search in the other table the number of times the id for the user model shows up and carry out your calculation with the results.
The booking mechanism would be sort of the inverse of this. In the holiday table, you want to make a method that creates an entry in the database with the user who booked the holiday, so that when the user runs the method detailed above, the value is update appropriately.

How do I hide fields thats rails admin shows when I create a new model (user)?

When I'm going to create a new user, rails admin shows me many fields like created_at, sign_in_at, etc, etc etc.
You need to use create instead of list in the config block.
Enter the following code in config/initiazlizers/rails_admin.rb.
See https://github.com/sferik/rails_admin/wiki/Fields for further details regarding the customization of fields.
config.model 'User' do
create do
field :name
field :email
# etc....
end
end

Model 2 way relationships to store id in user table

I'm trying to understand how I would go about storing the ID of one model record in a separate active record table (in this case the user table) when it gets created.
Basically, I have two models. A user which has_many :taxes and a tax model which belongs_to: user. In my application a user can only have one tax record, which currently I'm achieving by storing the user.id in a column in the tax model, and checking in a before filter to see if the user already has already created a tax record (which checks to see if their user.id is in the table.)
create_table "taxes", force: true do |t|
t.integer "user_id"
t.integer "income"
t.integer "taxes"
.....
Keep in mind this is all currently in an index action, which is kind of pointless since a user can only have one tax record, and only view their own tax records. It should be done with the show action I assume.
Now here's the problem, when a user creates their single tax record, the tax model is setup like this, all interacting with the taxes active record table:
def store_raw_taxes
tax = Tax.new(user_id: user.id, income: income, taxes: taxes, rrsp: rrsp)
tax.save
end
In order to use show (and check to see if the user already has created a tax record), I want to store the tax_id in the actual User's table in their row. I have created a migration AddTaxIdToUsers which made a reference and added a column called tax_id to the users table.
I don't know how to store the created tax record's ID in the user table though. This is a 2 way relationship I guess, but I don't understand how in my store_raw_taxes function, I would also interact with the User model. I guess it would be done in a after_save callback?
Would I also need to add belongs_to: tax to the user model in that case?
I would go a bit differently.
In a one-to-one relation you don't need the belongs_to definition, but the has_one
The conceptually right way to do it, in my point of view, would be:
user.rb:
has_one :tax
Then, your method would be
#user=User.find(..)
#user.build_tax(income: income, taxes: taxes, rrsp: rrsp)
Basically, I have two models. A user which has_many :taxes and a tax model which belongs_to: user. In my application a user can only have one tax record
If a user can only have one tax record, then you should not use has_many :taxes but has_one :tax instead. Rails will handle everything automatically.
See http://guides.rubyonrails.org/association_basics.html#the-has-one-association
Then you won't need "before filter to see if the user already has already". However is you want to ensure that user have a tax record at all times you can validates_presence_of :tax
See http://api.rubyonrails.org/classes/ActiveModel/Validations/ClassMethods.html#method-i-validates
It is possible, if you use nested form, that you don't need TaxesController at all.
Also if you use nested form you will be using accepts_nested_attributes and therefore will not need your store_raw_taxes method anymore
See http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html

Active Admin Model Index Page (Model List View) define order of displayed fields

Is there an easy way to change the order of displayed field of a Model in the Active Admin index view for a model? I have far more fields then Active Admin can display but i want to be able to define the most relevant fields for the user in this view.
The relevant fields in my case (see screenshot) are hidden (..) by Active Admin and only can be seen then i change to the show view of a model instance.
You should be able to define the fields to display in the index view inside the ActiveAdmin model configuration, i.e.
ActiveAdmin.register Place do
config.sort_order = 'name_asc'
index do
column :name
column :address
column :place_type
column :published
default_actions
end
end
You can read more about ActiveAdmin options here: http://www.activeadmin.info/docs/3-index-pages.html

How to map id to name in active admin index?

I am using active admin. I have project and user model with many to many relationship between them. In my user model i have a project_leader boolean column. And in my project model i have project_leader as integer column. I am allowed to select 1 project leader for each project. And then id of the user who is the project leader is stored in Project project_leader column. How do i map the id of the user to its name from active admin index?
Solved:
In my project model i defined a method as following.
def get_associated_user
p = self.project_leader
user = User.find(p).full_name
end
And from the active_admin index i just called the method object.get_associated_user
You can do it lik this too
column "Project Leader" do |p|
user = User.find(p.project_leader).full_name
end

Resources