Rails adding row to DB through admin approval - ruby-on-rails

I am making an application in Rails and I have Devise about to be included for users management. I have an index page where all the database rows are in with CRUD ability which is all good.
The functionality I'm after is that if a user wants to add a new row, they can go through and 'add' it but before it gets actually added, the request goes through to an admin, then the admin can hit 'approve' and then the row gets added.
The issue is me trying to get around the fact that the program would 'hold' an action of adding a row and the admin would approve it, then the program would let that action apply.
Thanks.

Instead of going through the hassle of preventing saving the row to the DB (you'd have to cancel the saving in a before_create callback but keep the data somewhere anyway and pass them to admin for approval somehow), I'd do a much simpler approach.
Add another column to the table, boolean column named e.g. approved and by default show only all approved rows in the listing. When a user creates a row, save it with approved = false and let admins access these unapproved rows and approve them simply by setting approved = true or delete them if approval is denied.
Also consider creating a DB index for this column for performance reasons.

Related

Sharing the variables between the actions in the controller in Rails

I am working on a module where I have to take the consent of the user to save the set of records.
those set of records are created in an action, which has to be made available in another action of the same controller, the records are being saved by the user consent.
now I can send these set of records to UI, from UI to again controller, if the user continues to save, if not cancel.
Problem is there will be thousands of records, which is painful to carry between UI and controller so My plan is to make the set of records available to the action which is being called by the continue button
the code
def create
#valid_members = generate_member_upload_results(params[:member_upload_user][:members_list])
end
in this action #valid_members is going to have the set of records. after this action executes in UI we will ask user whether the records are to be saved if no then cancels if yes then the following action will takes palce
def create_member
count = 0
unless #valid_members.blank?
#valid_members.each do |m|
count = count + 1
m.save(:validate => false)
end
end
redirect_to :back , notice:'#{count} members records created'
end
I want my #valid member should the same object which I used in create def.
I'm not entirely sure this is feasible with the flow you're suggesting. This sounds like something that could be resolved with a multi-step form but you would need to pass the data across or temporarily store it, which is seemingly what you're trying to avoid.
Alternatively, can you create a Rails endpoint that services the first step via javascript directly from the frontend? That can return the data without the user leaving the page, they can then confirm they are happy and submit the page once with approval.

ActiveAdmin - how to delete ALL objects (not only those on current list page) (Rails ')

On my ruby on Rails app using ActiveAdmin, I wish to delete not only the 30 Users displayed but all the 456 users (that's an example of course).
When I select "select all' and then confirm deletion, it only deletes the 30 users visible on the current screen page.
I want to select ALL users (across all view pages, not only the one I currently see), and then manually deselect the first 4 users (or any I would manually pick on the current view page). So not really deleting ALL users. that's my problem.
How to customize ActiveAdmin to be able to do this ?
Maybe something like this would work:
https://github.com/activeadmin-plugins/active_admin_scoped_collection_actions
Plugin for ActiveAdmin. Provides batch Update and Delete for scoped_collection (Filters + Scope) across all pages.
If you want to delete some users from a list of all of them, I suggest you to write a custom active admin action. Minimize your markup, make it easy to render for browser and prepare for the worst. If you have 1 million records, there is no way it will work properly, there is no solution for that.
I suggest you to accept the fact that user will delete records by using search, probably and if you literally want to be able to delete all you can provide a custom button delete all that will do that for you.
The alternative is write a custom active admin action with a lot of javascript to provide pagination. It's still a lot of custom code, no generic solution provided.
Last alternative, you can disable pagination for that active admin page, but you may have a lot of problems loading the entire table every time
You can override the default batch action to destroy/delete all the users like this:
ActiveAdmin.register User do
batch_action :destroy do |ids|
User.delete_all
redirect_to collection_path, alert: "Deleted all the Users!"
end
end
See this for more information.

Unique Session ids in Rails

I made a shopping cart model for a webapp that I am working on. It was just a standard ruby on rails model.
It lets users add products to the shoppingcart, but only shows it to them if they are the user that added the item. However, I'd like to let users who haven't signed in add items to a shopping cart.
Right now I'm using devise so I can check for a current_user, and assign that shopping cart item to that user. However is there a similar unique session id or anything I can use to simulate a user. So I only show the items that were added to the one person?
The reason for this is I'd like my user to go through as much of the ordering process as possible before asking them to sign-in and make an account.
Thanks
A simple method for this is creating a shopping cart and then adding that identifier to the session regardless of their logged in state.
For example:
#shopping_cart = ShoppingCart.create(user: current_user)
session[:shopping_cart_id] = #shopping_cart.id
Later you can retrieve the current shopping cart, if any, as a before_action handler.
I worked on similar problem. What I did was, I used session_id. First, you need a table(say, product_list) to hold the product list for temporary purpose. Populate each product to this table along with session_id. This will help you to identify the association of product_list with the active user. On checkout copy the record from product_list to your shopping_cart table and delete the record from product_list.
Try this, it might help you with the current problem.

Pass Account ID to New User During Sign Up

Currenntly, my application is designed using Devise for authentication. I have it so when the first user signs up, an account is created in an Accounts table and the account_id is passed to the User table. I also have it set so that each time a new account is created that user is tagged as an admin. Finally, I have it working where the admin can create new users.
My problem is that at the time the new users are created I need to have these users be assigned the same account_id as the admin to tie the users together. I can do this if I add an account_id field on the form and have the admin manually enter it. What I want to have is that this is automated in the background.
I tried many varieties without success. This is one of the unsuccesful attempts where I put the following in the user.rb
before_save :add_account_id_from_parent
def add_account_id_from_parent
return true unless self.users.present?
self.users.update_attribute(:account_id, 1)
end
I used the number "1" just to see if I could get anything automated and placed in that field.
Like I said manually everything works, but I want it so the acocunt_id is automatically added during sign up based on the admins account_id.
I'm a bit confused why you are calling self.users. If I understand correctly, you want to assign account_id to 1 after a new user is created (as a test). You can do that like this:
before_save :add_account_id_from_parent
def add_account_id_from_parent
self.account_id = 1
end
You don't need to actually update the record since this is assigned before save, and save will write the new value to the db.
Again I might be missing something, if so please clarify.
UPDATE:
If you're validating that account is present, you'll need to change the callback to a before_validation instead of before_save, like so:
before_validation :add_account_id_from_parent

Rails 3 - How to Create a Record if One Does not Exist OR Update the existing records

I'm creating a permissions table for People & Books
In the permissions table I have: Permission.ID, user.id, book.id
I want an admin to be able to set permissions for Users<>Books.
When the user selects the permissions and clicks submit, in the Rails controller, should I be submitting to /create or /update?
is there a way I can submit to just one whether it's new or an update, and let Rails know to either Create or Update a record based on if a record exists per a UserID And BookID?
Thanks
If they're editing an existing record just submit to update. If they are creating a new record and you're making sure there aren't any duplicates, try using find_or_create_by inside your create method.

Resources