Swagger UI - Limit enum array to only a single submittable value - swagger

I am creating a pre-defined list of items that I would like the user to select from within Swagger UI and I found the layout for the most part, however with the way I have it setup, the user can submit multiple values in the enum whereas I only want the user to be able to select a single value. Any recommendations would be appreciated. Right now the user can select from "available", "pending", or "sold". I have included the "required" parameter so I am handling the case where the user needs to select something at least, now I just need to restrict it to only a single value.
Swagger UI config:
{
"name": "event",
"in": "query",
"description": "events that need to be considered for filter",
"required": true,
"style": "form",
"explode": true,
"allowEmptyValue": false,
"schema": {
"type": "array",
"items": {
"type": "string",
"default": "available",
"enum": [
"available",
"pending",
"sold"
]
}
}
}

Restrict the user to only the values your looking for. You need to loop through their response against your accepted response. If acceptable and matches' your parameters, move on, else select a different response.

Related

Check if all (hidden) elements contain a string with Minitest/Capybara

I have a list of elements on a page (cards with comic books) and I want to test if all elements have the title containing a string (let's say "spiderman").
<%= #comics.each do |comic| %>
<div class="comic-card-text-box">
<p class="comic-card-title"><%= comic.title %></p>
</div>
<% end %>
Using Minitest/Capybara/Rails 7, I'm trying to do something like this:
test "displays search results" do
login_as users(:user1)
visit root_path
fill_in "Search...", with: "spiderman"
assert_selector ".comic-card-title" do |element|
assert_match element, /spiderman/
end
end
And that's the failure I get:
expected to find visible css ".comic-card-title" but there were no matches. Also found "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", which matched the selector but not all filters.
I have 2 issues there that I'm confused about.
I am trying to check if an element contains a substring. What if this element is hidden on the page (only by hovering it will appear)?
How to iterate between these elements to check if all of them have the substring?
Your elements aren't matching either visibility or the filter proc you're passing to assert_selector (don't use assert_match with Capybara elements). If that's because the elements need to be hovered over then you should really just hover over them. Also assert_selector just validates that at least one of the elements will match, not that all of them will. From your questions I'm assuming the comic-card-text-box element is visible on the page (takes up some space) and that the comic-card-title element only becomes visible when you hover over the comic-card-text-box element. If that's the case then you'd want to do something like
cards = all('.comic-card-text-box', minimum: 1) # specifying count: <expected count> rather than minimum would be better
cards.each do |card|
card.hover
assert_css(card, '.comic-card-title', text: 'spiderman')
end
If my assumptions about what exactly you're trying to do, and your sites behavior are wrong please clarify in your question.

How to update multiple users with form in Rails?

I want to use form_for to create a form and update multiple users.
Let's say each user has :id and :name. I would like the POST parameters to be:
{
"users": [
{
"id": 1,
"name": "Bob"
},
{
"id": 2,
"name": "Leo"
}
......
]
}
The form looks like([] means input fields):
Users
1 [ Bob ]
2 [ Leo ]
3 [ ]
How does the form_for code looks like?
form_tag your_url, method: :post do |f|
#users.each do |user|
label_tag user.id
hidden_field_tag "users[][id]", user.id
text_field_tag "users[][first_name]", user.first_name
end
end
I think it will pass parameters as you want, but the hard thing is how to show detail error messages for each user

Rails - select_tag form helper is not working properly

I just recently started working in rails forms, and though I have scoured the api docs many times, I have not been able to figure out how to use it properly.
Basically, in my app I have a form to place a manual order (shipping). I would like there to be a select tag for specifying the state to be shipped to. I am working with an pre-existing SQLServer database, and the column I would want to display, and edit is :SHIPTOSTATE . However, when I go to edit an instance of an order, the form always shows "AL" and changing the state does not actually update the shipping state in my form. Can someone point out what I am doing wrong here?
The rest of my form is working beautifully, as the form is mostly text-fields at this point. The text-fields update just fine, but the select_tag is still troublesome.
Here is a snippet of the code in question:
<%= select_tag :SHIPTOSTATE, options_for_select(["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY"]) %>
Did you add :SHIPTOSTATE to permit method inside controller?
select_tag on it's own does not know which object you are attaching to. You need to provide a current value. You can do that with adding a second parameter to the options_for_select call.
options_for_select(["AL","AK"...], "Current State")
Then you have to explicitly look at the params[:SHIPTOSTATE] in your controller method.
You might also be using form_for. In that case you would want to use something like f.select and not use select_tag at all:
<%= form_for #your_object do |f| %>
# ...
<%= f.select :SHIPTOSTATE, ["AL","AK"...]
# ...
<%= f.submit %>
<% end %>
When you're dealing with a model, using the conventions in Rails, it's rare that you'd use the select_tag helper - you'd usually use the select helper, something like this:
= f.select :SHIPTOSTATE, %w[AL AK AZ AR ..etc]
If changing to using that helper doesn't work for you then you'll need to show us your controller, and perhaps your model schema, so we can see what else might be wrong here.

Ruby/Rails: each do loop range from another loop

I'm using the following each do loop to pull in data from a JSON file and make it usable on my site.
<% data.games.ronedoneb.each do |s| %>
This is working great. What I'm wanting to do is to specify a range that will be used, like so:
<% data.games.ronedoneb[(0..5)].each do |s| %>
What I'm wanting to do however, is change the range (0..5) based on fields within another JSON file, the range will always be blocks of 6 so: (0..5), (6..11), (12..17) etc etc.
This is what I've tried to do is below:
<% data.games.ronedoneb[(<%= ss[:z1] %>..<%= ss[:z2] %>)].each do |s| %>
This doesn't work, I hoped that I'd be able to pull the z1 and z2 results from the first JSON file.
Is there a way that I can do this? Is there something I'm missing?
Below are examples of the JSON being used.
JSON 1
"Ar": "1",
"Br": "0",
"Round": "1",
"Game": "3",
"Date": "Thursday, 5 February 2015",
"Day": "1",
"z1": "12",
"z2": "17"
JSON 2
"Game": "1",
"AR": "9",
"Day": "1",
"GPMB": "351",
"DR": "2",
"CSB": "275",
"GPMR": "360",
"AB": "1",
"Round": "1",
"CSp10R": "60",
"GoldR": "13.2",
"DB": "2",
"CSR": "222",
"GoldB": "12.9",
"KDAR": "6.50",
"Blue": "23.7",
"CSat10B": "79",
"KB": "5",
"KDAB": "3.00",
"KR": "4",
"CSat10R": "76"
You can't use <%= in the place you did. Please try something like:
<% data.games.ronedoneb[(ss[:z1].to_i..ss[:z2].to_i)].each do |s| %>
You can use ruby's already built in enumerable method each_slice you specify the slice length and it will cut the array into slices with that length, then pass the slices to the block.
You'll probably need to convert the json into a hash, but that's easy,
data.games.ronedoneb.each_slice(5) do |slice|
slice.each do |item|
#process here
end
end

Using Rails as_json with conditional statement

I have a file (show.json.erb) in which I'm formatting a model as json. In my app, Projects have many Steps and many Images, and Steps have many Images.
I'd like to be able to include only a default image for a project, which I'm trying to do using "conditions." However, it seems to be ignoring the conditions and posting all the images associated with a project. How do I include only the right images?
"projects":
<%= JSON.pretty_generate(#projects.order("updated_at DESC").as_json(only: [:title, :id,
:built, :updated_at], include: {images: {only: [:image_path],
:conditions=>['Step.find(image.step_id).name.eql? "Project Overview"', true] }
})).html_safe %>
I solved this problem by creating a method called image_path in my Projects model:
(I needed the method to return three types of images)
def image_path
images = Hash.new
if first_step.images.present?
images["url"] = first_step.first_image.image_path_url
images["preview"] = first_step.first_image.image_path_url(:preview)
images["thumbnail"] = first_step.first_image.image_path_url(:thumb)
end
return images
end
Then I edited my JSON to look like this:
"projects":
<%= JSON.pretty_generate(#projects.order("updated_at DESC").as_json(only: [:title, :id, :built], :methods=> [:image_path, :updated_at_formatted])).html_safe %>
This gets rendered like this on my website:
"projects":
[
{
"built": true,
"id": 115,
"title": "Video Test",
"image_path": {
"url": "https://s3.amazonaws.com/...",
"preview": "https://s3.amazonaws.com/...",
"thumbnail": "https://s3.amazonaws.com/..."
},
"updated_at_formatted": "07/08/2013 10:31:19"
},
...]
Hope this helps someone else!

Resources