How do you add nested attributes in Savon 2.3? - savon

Savon 2.1 used :attributes! to add attributes in nested xml tags. How is it done in 2.3? The exact same hash does not render the same xml.
{
:person => {
:address => "",
:attributes! => { :address => { :id => 44 } }
},
:attributes! => { :person => { :id => 666 } }
}
creates
<person>
<address id="44"/>
</person>
<attributes>
<person>
<id>666</id>
</person>
</attributes>
instead of
<person id=\"666\"><address id=\"44\"></address></person>
For reference:
How do I use savon nested attributes! hash?
Another example where even the inner attributes! fails
{
'Objects' => [{
'EmailAddress' => 'CreatedUser#test.com',
:attributes! => {
'EmailAddress' => { 'xsi:type' => "tns:email" }
}
}],
:attributes! => {
'Objects' => { 'xsi:type' => "tns:Subscriber" },
}
}
Produces:
<Objects>
<EmailAddress>CreatedUser#test.com</EmailAddress>
<attributes>
<EmailAddress>
<xsi:type>tns:email</xsi:type>
</EmailAddress>
</attributes>
</Objects>
<attributes>
<Objects>
<xsi:type>tns:Subscriber</xsi:type>
</Objects>
</attributes>
Later example and ultimate reasoning for issue is attempting to create a subscriber for ExactTarget.
https://webservice.exacttarget.com/etframework.wsdl
61 def soap
62 #soap_client ||= Savon.client(
63 soap_header: header,
64 wsdl: 'https://webservice.exacttarget.com/etframework.wsdl',
65 endpoint: endpoint,
66 wsse_auth: ["*", "*"],
67 raise_errors: false,
68 log: true,
69 open_timeout: 180,
70 read_timeout: 180,
71 pretty_print_xml: true
72 )
73 end
112 def create_subscriber
113 soap.call :create, :message => {
114 'Objects' => [{
115 'EmailAddress' => 'CreatedUser#test.com'
116 }],
117 :attributes! => {
118 'Objects' => { 'xsi:type' => "tns:Subscriber" },
119 }
120 }
121 end
Header omitted since it contains credentials.

I ended up getting like so:
soap.call :create, :message => {
'Objects' => [{
'EmailAddress' => 'CreatedUser#test.com',
:'#xsi:type' => "tns:Subscriber"
}]
}
Attributes are flagged in the same level by using #

Related

only strings in influxdb

i've this config file in logstash
input {
redis{
host => "localhost"
data_type => "list"
key => "vortex"
threads => 4
type => "testrecord"
codec => "plain"
}
}
filter {
kv {
add_field => {
"test1" => "yellow"
"test" => "ife"
"feild" => "pink"
}
}
}
output {
stdout { codec => rubydebug }
influxdb {
db => "toast"
host => "localhost"
measurement => "myseries"
allow_time_override => true
use_event_fields_for_data_points => true
exclude_fields => ["#version", "#timestamp", "sequence", "message", "type", "host"]
send_as_tags => ["bar", "feild", "test1", "test"]
}
}
and a list in redis with the following data:
foo=10207 bar=1 sensor2=1 sensor3=33.3 time=1489686662
everything works fine but every field in influx is defined as string regardless of values.
does anybody know how to get around this issue?
The mutate filter may be what you're looking for here.
filter {
mutate {
convert => {
"value" => "integer"
"average" => "float"
}
}
}
It means you need to know what your fields are before-hand, but it will convert them into the right data-type.

Rails 3 parent and child association scopes optimization

I have a parent has many children association in my project. I'm trying to get a list of children that have certain flags set from the parent when I render it.
For example this:
# some_controller
#thread = Thread.includes(:posts)
render :json => #thread.posts.as_json
will return something like
[
{
"id" => 1,
"posts" => [
{ "id" => 1, "flagged" => true },
{ "id" => 2, "flagged" => false }
]
}
]
I'm trying to get the output to be like this
[
{
"id" => 1,
"posts" => [
{ "id" => 2, "flagged" => false }
]
}
]
i.e.: only posts that are not flagged should be rendered.
I've overridden the as_json method in my model in order to do this as such:
def as_json(options = {})
...
{
:id => self.id,
:posts => self.posts
}
...
end
I realize that I can define a scope in the Posts model and use :posts => self.posts.not_flagged, but this causes an extra database query for every thread in my database (makes everything slow).
Aside from a default_scope on the Posts model how else could I solve this extra query problem?
I figured it out a bit after I posted.
I can add a scope in my Parent model to do this:
# Thread.rb
...
has_many :unflagged_posts, :class_name => "Post", :conditions => { :flagged => false }
...
And in my controller:
# some_controller
#threads = Thread.includes(:unflagged_posts)
This way ActiveRecord eager loads the posts with the flagged condition, rather than doing an extra query for each post!

Rails 3: How do I validate to allow blanks ("") but not nil (NULL in database)

In an ActiveRecord (or ActiveModel) I would like the following spec to pass
it { should allow_value("").for(:my_string) }
it { should_not allow_value(nil).for(:my_string) }
I have tried
validates :my_string, {
:length => { :in => 0..255 },
:presence => true,
:allow_blank => true,
:allow_nil => false,
}
and also
validates :my_string, {
:length => { :in => 0..255 },
:allow_blank => true,
:allow_nil => false,
}
But either it allows both "" and nil or none of them.
This works for me
validates :my_string, length: { in: 0..255, allow_nil: false }
If you just want to validate that the field is not null, but don't care about blank/empty strings, this works:
validates :my_string, length: { minimum: 0, allow_nil: false, message: "can't be nil" }
You can try doing:
validates :my_string, exclusion: { in: [nil]}
It is part of the validations themselves in ActiveRecord.
I've tried the others and all of them are very compliated or allow nil too.
You might need to do a custom validation for this:
validates :my_string, :length => { :in => 0..255 }
validate :my_string_is_valid
def my_string_is_valid
self.errors.add :base, 'My string can not be nil' if self.my_string.nil?
end
You can create a simple custom validator (placed in app/validators dir)
class NotNilValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
record.errors[attribute] << "must not be nil" if value.nil?
end
end
and then
validates :my_string, not_nil: true
Or maybe:
validates :my_string, :length => { :in => 0..255 }, :allow_nil => false
Seems that allow_nil does not override allow_blank.
So you better no specify allow_blank
This works for me:
validates_exclusion_of :my_string, in: [nil]

Netzke basepack. Need advice with multi uploading fields

Is there any easy way to include the multiupload feature to NetzkeFormView or GridView(AddInForm)?
My current image uloading field with carrierwave is:
{:name => :image_link, :xtype => :displayfield, :display_only => true, :getter => lambda { |r| %Q(<a href='#{r.image.url}'>Download</a>) if r.image.url }},
{:name => :image, :field_label => "Upload image", :xtype => :fileuploadfield, :getter => lambda { |r| "" }, :display_only => true}

mongoid query rails 3

brand new to MongoDB / mongoid and still pretty green on ruby / rails
on the rails console:
t = Task.all(:conditions => {:title => "task2"})
=> #<Mongoid::Criteria
selector: {:title=>"task2"},
options: {},
class: Task,
embedded: false>
>> t.first
=> #<Task _id: 4d7e2cdb73ec3227dd000009, title: "task2", latitude: 23.987904829, longitude: -12.9423875, created_by: "4d792d6973ec3248e3000001">
returns what I would expect, but
t = Task.where(:created_by => "4d792d6973ec3248e3000001")
=> #<Mongoid::Criteria
selector: {:created_by=>BSON::ObjectId('4d792d6973ec3248e3000001')},
options: {},
class: Task,
embedded: false>
>> t.first
=> nil
same result with:
t = Task.all(:conditions => {:created_by => "4d792d6973ec3248e3000001"})
here is the model:
class Task
include Mongoid::Document
field :title
field :latitude, :type => Float
field :longitude, :type => Float
field :created_by
end
what am I doing wrong ?
updated example:
>> Task.new(:title => "task8", :created_by => "4d792d6973ec3248e3000001")
=> #<Task _id: 4d81037973ec32cc22000003, latitude: nil, longitude: nil, title: "task8", created_by: "4d792d6973ec3248e3000001">
>> t = Task.all(:conditions => {:created_by => "4d792d6973ec3248e3000001"})
=> #<Mongoid::Criteria
selector: {:created_by=>BSON::ObjectId('4d792d6973ec3248e3000001')},
options: {},
class: Task,
embedded: false>
>> t.first
=> nil
do I need to specifically define created_by as :type => String ?
Looks like created_by is stored as a string in your document, but then you query looking for an ObjectId. ObjectId's and strings aren't the same.
Try changing
>> Task.new(:title => "task8", :created_by => "4d792d6973ec3248e3000001")
to
>> Task.new(:title => "task8", :created_by => BSON::ObjectId("4d792d6973ec3248e3000001"))
You have to include this functionality in mongoid as explained in docs:
class Person
include Mongoid::Document
include Mongoid::Timestamps
end