How to set a property in ruby datamapper that is a function of another property of the same object? - ruby

I want to add two new properties to a ruby datamapper model, one that is a date cast of a timestamp property and another that is a value from another object connected through a unique key.
So for the first case I have
property :date, DateTime
and I want to add another
property :date_date, Date
that by default will be equal to date.to_date

You should see the docs: https://datamapper.org/docs/properties.html
Specifically the sections on "Available Types" and "Setting default values".
You can do it like this:
property :date_date, Date, default: -> do |obj, prop|
obj.date.to_date
end
You can alternatively set it through a callback (https://datamapper.org/docs/callbacks.html), for example:
property :date_date, Date
before_save :set_date_date
def set_date_date
self.date_date = date.to_date
end
Note it works basically the same way in Rails' ActiveRecord as well.

Related

Remove enum value with gem sequel

I have a db - Postgresql v11.3 and Sequel with pg_enum, so how I can remove value from enum type?
For example, need change column enum type :
Sequel.migration do
up do
removed_values = %w["val1" "val2"]
remove_enum_value= (:old_enum_type, removed_values)
create_enum(
:enum_column,
%w[val3 val4]
)
alter_table :users do
set_column_type(
:enum_column,
'enum_column[]',
using: 'old_enum_column::text[]:::enum_column[]'
)
set_column_default :enum_column, '{}'
end
end
end
New enum type same as previous, with little different - new type doesn't have a some values. But, may be situation if somebody use missing values - migration will be crashed.
Unfortunatly, pg_enum don't have a method, that will able simple remove enum value
If you review https://www.postgresql.org/docs/12/sql-altertype.html, you will see that PostgreSQL supports adding and renaming enum values, but not removing them.

Clear input field: undefined method `clear' for #<Watir::Input:XYZ> (NoMethodError)

I am not sure why I can not clear my input field.
PageObject:
element(:test_input_field) { |b| b.input(class: "search-field") }
def set_search_value(search_entry)
test_input_field.when_present.clear
test_input_field.when_present.set(search_entry)
end
Step_file:
page.set_search_value(search_entry)
Output:
undefined method `clear' for #'<'Watir::Input:0x00000003980d20'>' (NoMethodError)
The clear (and set) method are not defined for generic input elements - ie Watir::Input. They are only defined for the specific input types - text field, checkbox, etc.
To make the code work, you would need to convert the input into the more specific type, which is likely a text field. You can do this using the to_subtype method:
test_input_field.when_present.to_subtype.clear
test_input_field.when_present.to_subtype.set(search_entry)
As #SaurabhGaur mentions, set already starts by clearing the existing value, so you could just do:
test_input_field.when_present.to_subtype.set(search_entry)
Unless the input type changes, it would make more sense to define the element as a text_field so you do not need to convert it. It might depend on which page object library you are using, but I would expect you could do:
element(:test_input_field) { |b| b.text_field(class: "search-field") }
def set_search_value(search_entry)
test_input_field.when_present.clear
test_input_field.when_present.set(search_entry)
end

Getting model value and generated ID from within simple_form custom input

I'm trying to make a custom input type with simple_form that will implement combobox-type functionality using jQuery-Autocomplete
. What I need to do is output a hidden field that will hold the ID of the value selected and a text field for the user to type in.
Here's what I have so far:
class ComboboxInput < SimpleForm::Inputs::Base
def input
html = #builder.hidden_field(attribute_name, input_html_options)
id = '' #what?
value = '' #what?
return "#{html}<input class='combobox-entry' data-id-input='#{id}' value='#{value}'".html_safe
end
end
I need to get the ID of the hidden field that simple_form is generating to place as an HTML attribute on the text entry to allow the JavaScript to "hook up" the two fields. I also need to get the value from the model to prepopulate the text input. How do I do this from within my custom input?
I'm looking for the id as well, but I did get the value:
def input
current_value = object.send("#{attribute_name}")
end
I just found a hokey id workaround:
html = #builder.hidden_field(attribute_name, input_html_options)
id = html.scan(/id="([^"]*)"/).first.first.to_s
I know it's a hack, but it does work. Since we don't have access directly to this type of resolution, it is likely to keep working even if the underlying id creation code changes.

uninitialized constant OCI8::Object::Mdsys::SdoGeometry

I need to patch oracle enhanced adapter for some reasons.
In my oracle_enhanced_adapter.rb file, I need to get OCI8::Object::Mdsys::SdoGeometry.new return value, but it returns uninitialized constant OCI8::Object::Mdsys::SdoGeometry.
But, if before executing OCI8::Object::Mdsys::SdoGeometry.new command, I execute OnlineGpsPoint.first command (OnlineGpsPoint contains an sdo_geometry column) and get one of the rows in the table, OCI8::Object::Mdsys::SdoGeometry will be initialized.
The problem is that I don't want hardcoded 'OnlineGpsPoint' in my program. I just need OCI8::Object::Mdsys::SdoGeometry value.
Is there a solution for the pain?
When ruby-oci8 finds an unknown object type, it automatically defines a ruby class under OCI8::Object. If you know the object type name, it is better to define the ruby class in advance as follows:
# Oracle object type name is *guessed* from the ruby class name.
# SdoGeometry => SDO_GEOMETRY ==(public synonym)=> MDSYS.SDO_GEOMETRY
class SdoGeometry < OCI8::Object::Base
end
or
# Set Oracle object type name explicitly
class AnyClassName < OCI8::Object::Base
set_typename('MDSYS.SDO_GEOMETRY')
end
If you want same name with the automatically generated class:
module OCI8::Object::Mdsys
class SdoGeometry < OCI8::Object::Base
set_typename('MDSYS.SDO_GEOMETRY')
end
end

Comma separated numbers in rails 3.0.5 project

In my rails project I have a form to enter a dollar amount. This amount is stored as an integer in a mysql database.
<%=f.label :Amount%><br/>
<%=f.text_field :amount%>
if the user enters the number using a "." separator like 2034.34 it works well. My problem is when someone uses a "," as in 2,034. This number is stored just as a 2 in the databases. How do I get the app to store number with both comma and decimal separators?
Update
Nkm put on the right track but I got a stack too deep error. I ended up using
def amount=(amt)
write_attribute(:amount,amt.gsub(",", ""))
end
Since the database column is decimal/float, we should process the data into the right format before saving.
You can either go with activerecord before_save callback or override the setter method of that attribute as follows,
#model.rb
def amount=(amt)
self.amount = amt.gsub(",", "")
end
You may add before_save callback in your Model to convert commas to dot .
before_save { self.amount.gsub!(/\,/, "." }
irb example:
"2,034".gsub!(/\,/, ".")
# => "2.034"
You ask to store the amount as a formatted String in the database. I think it's a really bad idea: it will be difficult to do calculation with this string (sums, comparison, etc...). Use a float column instead.
You can use a callback (I hope they're available in 3.0.5) on your ActiveRecord model to remove the comma before saving. For example
before_save :format_amount
def format_amount
self.amount.delete! ','
end
Use NumberHelper to display back the comma in your view.

Resources