Checking rails form data against key before saving to database - ruby-on-rails

I'm attempting to write an app that allows users to register different id numbers to their account through the use of a form. The ids have their own model and the table is as follows:
mysql> show columns in hp_ids;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_string | varchar(255) | YES | | NULL | |
| pin_number | int(11) | YES | | NULL | |
| user_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+------------+--------------+------+-----+---------+----------------+
The id number is saved under id_string
The trick is that, in order to register an id, a user must enter its corresponding pin to prove that they have the right to register it..
What I want to do is, each time someone attempts to register a new id/pin combination, I would like to check it against a key of sorts to make sure that they have the right combination. I figure I would create a new model called IdKey or something like that but I can't figure out how I would tell the controller to check the form values against this second model prior to saving them.
Could anyone point me in the right direction?

It sounds like a conditional or custom validation.
I may be misunderstanding your question, but is pin_number a preexisting value? If so, I'd guess it should exist in a different table (which could be queried during your custom validation).

Related

Can you use parameter/variables/placeholders for values for future use in a Specflow scenario?

In previous job I have used DBFit and used parameters (variables/placeholders) for values
example:
|Key? |
|>>Key|
!|Query|SELECT Status FROM Confirm WHERE Name='xyz' |
| Status | Key |
| Confirmed | <<Key |
I am now using SpecFlow and wondered if it has similar functionality
example: ( I have used << and >> here just for explanation )
Given I get Initial for
And the 1st response should contain
| Name | string | "xyz" |
| Key | string | >>{Key} |
When I get Confirm for
Then the 1st response should contain
| Name | string | "xyz" |
| Key | string | <<{Key} |
I think you are looking for Scenario Outlines.
With them, you can specify a table with your parameters. So in your case, it is looking something like this:
Scenario Outline: Title for your Scenario Outline
Given I get Initial for And the 1st response should contain
| field | type | assertion |
| Name | string | "xyz" |
| Key | string | <Key> |
When I get Confirm for
Then the 1st response should contain
| field | type | assertion |
| Name | string | "xyz" |
| Key | string | <Key> |
Examples:
| Key |
| example1 |
| example2 |
| example3 |
Be aware that you have here two different types of tables. The table at your steps is an argument for the step.
The Examples table at the end are the concrete examples. So the Scenario is executed once per each entry in this table.
You can use the parameters from the example table with a simple <COLUMN_NAME> placeholder.
Docs: https://docs.specflow.org/projects/specflow/en/latest/Gherkin/Gherkin-Reference.html#scenario-outline

How to get review cycle duration information from gerrit?

I'm trying to get some data on how long it takes for reviews to go through Gerrit on average.
Looking at some open source code, I see stuff like
reviewCreateTime = moment(mergedReviewsList[review].created);
reviewUpdateTime = moment(mergedReviewsList[review].updated);
interval = reviewUpdateTime.diff(reviewCreateTime, TIME_PERIOD_TYPE);
But with experimentation I don't think this logic is correct because adding a comment to a merged CR changes the updated timestamp.
I know this is possible because at the time of merge, Gerrit prints to the UI Change has been successfully merged by XXX.
I've been digging around in the mysql database but haven't found anything useful. I notice that changes that have been submitted have a submission_id, but I haven't found a table that stores submission information.
After a bunch of digging around, I have come up with one rather ugly but workable solution.
There is a table change_messages
mysql> describe change_messages;
+-----------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+-------------+------+-----+---------+-------+
| author_id | int(11) | YES | | NULL | |
| written_on | timestamp | NO | | NULL | |
| message | text | YES | | NULL | |
| patchset_change_id | int(11) | YES | MUL | NULL | |
| patchset_patch_set_id | int(11) | YES | | NULL | |
| change_id | int(11) | NO | PRI | 0 | |
| uuid | varchar(40) | NO | PRI | | |
+-----------------------+-------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
This basically stores stuff like XXXX has been successfully merged by YYYY and XXXX has been successfully cherry-picked as YYYY by ZZZZ.
You can then join this table with changes and datediff on change_messages.written_on and changes.created_on, e.g.
SELECT changes.change_id,
created_on,
written_on,
Datediff(written_on, created_on) diff
FROM change_messages
INNER JOIN changes
ON change_messages.change_id = changes.change_id
WHERE message LIKE 'Change has been successfully merged by %'
ORDER BY written_on;
Now this includes any time the CR was in draft mode. I'll edit this question if I get around to excluding that time.

how to assign a posted file to a Paperclip model in the controller

I am trying to have a one-off image upload functionality for part of a site (via ajax). Right now, the posted values look like this:
Processing by ApiPostsController#upload_image as */*
Parameters: {"file"=>#<ActionDispatch::Http::UploadedFile:0x007feb23c28420 #original_filename="yoko.jpg", #content_type="image/jpeg", #headers="Content-Disposition: form-data; name=\"file\"; filename=\"yoko.jpg\"\r\nContent-Type: image/jpeg\r\n", #tempfile=#<File:/var/folders/58/8l1dcc5n6dd_7mhqvqr_3zm40000gn/T/RackMultipart20140107-70298-1mih9mx>>}
this is auth_token WgfFAEz3JIApcWDjz2dRgA
I'd like to do something like this in the controller:
def upload_image
#asset=Asset.new
#asset.asset_file_name=params[:filename]
#asset.save!
r={}
r[:status]="success"
render text: r.to_json
end
Our Asset model uses asset as name (unfortunately).
mysql> describe assets;
+----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
| asset_file_name | varchar(255) | YES | | NULL | |
| asset_content_type | varchar(255) | YES | | NULL | |
| asset_file_size | int(11) | YES | | NULL | |
| asset_updated_at | datetime | YES | | NULL
I'm trying to follow the readme for paperclip where it says:
Paperclip will wrap up up to four attributes (all prefixed with that
attachment's name, so you can have multiple attachments per model if
you wish) and give them a friendly front end. These attributes are:
_file_name
_file_size
_content_type
_updated_at
By default, only _file_name is required for paperclip to
operate.
But I get this error
ActiveRecord::RecordInvalid (Validation failed: Asset can't be blank):
app/controllers/api_posts_controller.rb:21:in `upload_image'
How could I get paperclip to accept this uploaded file?
Try:
#asset.asset = params[:file]
What is written in a doc means that you can add those four columns to your models and they all be auto-populated on saving an attachment. asset_file_name is the only required column and it will contain the name of new file created on a server.
If you assign file_name alone, you will only fill one column in your db, but the file itself is not being saved on server's disk - you need to assign the whole file and paperclip will take care of the rest.

Rails - Multiple file upload form with additional input for each file

I'm working on an app at work where users can upload files to a project. Right now I'm using paperclip, and all of the file info (name, file type, etc.) is recorded properly.
The problem I'm having is that the users need to assign a category to each file.
What is the best approach to implement something like this? I'm on my phone right now, but I'll attach any pertinent code when I get to work, if necessary.
Thanks in advance!
EDIT
Here's more info on the setup.
I need to assign the file type (which is more like a category than a file type. I didn't name the fields!) So all of the info is in a single table. Although accepts_nested_attributes will definitely come in handy down the road!
Here's the table structure. All of the document_ fields are set by paperclip. The only one that needs user input is the file_type field.
+-----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| document_file_name | varchar(255) | YES | | NULL | |
| document_content_type | varchar(255) | YES | | NULL | |
| document_file_size | int(11) | YES | | NULL | |
| document_updated_at | datetime | YES | | NULL | |
| project_id | int(11) | YES | MUL | NULL | |
| user_id | int(11) | YES | MUL | NULL | |
| file_type | varchar(255) | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
+-----------------------+--------------+------+-----+---------+----------------+
According to Zippie, you can check Nested Model form. I'm also not sure if it works for files. You can get ideas from this tutorials.

[AnyDac][DApt]-400 But my tables does have a PK

[anydac][DApt]-400.Fetch command fetched[0] instead of [1] record,
Possible reasons:update table does not have PK or row identifier,record has been changed/deleted by another user,
when executing
SingleTestRunADQuery.Append();
SingleTestRunADQuery.FieldByName('run_id').Value := StartRecordingButton.Tag;
SingleTestRunADQuery.FieldByName('ph_value').Value := FloatToStr(ph_reading);
SingleTestRunADQuery.FieldByName('conductivity_value').Value := conductivity_reading;
SingleTestRunADQuery.FieldByName('cod_value').Value := cod_reading;
SingleTestRunADQuery.Post();
on
mysql> describe measurements;
+------------------------+-----------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------------+-----------+------+-----+-------------------+-------+
| run_id | int(11) | NO | MUL | NULL | |
| measurement_time_stamp | timestamp | NO | PRI | CURRENT_TIMESTAMP | |
| ph | float | NO | | NULL | |
| conductivity | float | NO | | NULL | |
| cod | float | NO | | NULL | |
+------------------------+-----------+------+-----+-------------------+-------+
5 rows in set (0.03 sec)
as you can see, the table does have a PK. Also, the program is single-threaded and only one copy is running, so no one else is updating.
I set SingleTestRunADQuery.MasterFields=run_id and IndexFieldNames=run_id as that is the PK of table which holds a summary of all test runs. The second table hold the measurements taken during tests, with run_id giving all the measurements for one test run (I only added PK on tiemstamp to get rid of this error, but it didn't work and can be removed, I guess).
In case it helps, here's the master data source:
mysql> describe test_runs;
+------------------+-------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+-------------------+----------------+
| run_id | int(11) | NO | PRI | NULL | auto_increment |
| start_time_stamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| end_time_stamp | timestamp | YES | | NULL | |
| description | varchar(64) | YES | | NULL | |
+------------------+-------------+------+-----+-------------------+----------------+
4 rows in set (0.05 sec)
Any idea what's wrong?
[Update] # mj2008 points out that some fields have different names. This is for historical reasons (I am still trying something out & don't want to change yet), hoever these are adapted by the query:
SELECT run_id,
measurement_time_stamp,
ph as ph_value,
conductivity as conductivity_value,
cod as cod_value
FROM photo_catalytic.measurements
ORDER BY measurement_time_stamp DESC
I'm not sure that is correct to have TIMESTAMP field as PRIMARY KEY. It will automatically change on every UPDATE.
Check out to the TFields property of Query component, through "Fields Editor" option. check that "Key Fields" has a ProviderFlags.pfInKey property set to true.
This applies today to FireDac components.
You should change the connection properties as follows: Try setting UpdateOptions.RefreshMode to rmManual.
enter image description here
check if timestamp that is on database doesnot hav seconds and miliseconds

Resources