Rails print ruby string in haml code - html

Hey I am fairly new to web programming, so please excuse my ignorance.
I have the following code in a .html.haml view file.
%video{:controls => "controls", :height => "240", :width => "320"}
%source{:src => #video_resource.file_path, :type => "video/mp4"}/
%source{:src => #video_resource.file_path, :type => "video/webm"}/
%source{:src => #video_resource.file_path, :type => "video/ogg"}/
%object{:data => "flashmediaelement.swf", :height => "240", :type => "application/x-shockwave-flash", :width => "320"}
%param{:name => "movie", :value => "flashmediaelement.swf"}/
%param{:name => "flashvars", :value => "controls=true&file=movie.url"}/
= t('html5_video_error')
How do I get #video_resource.file_path into the flashvars object param file variable (currently it is set to movie.url). I may be misunderstanding the way this works.

What you need here is basic string interpolation, the ruby on rails syntax is the following:
# haml code:
%span= "I am a #{ 'interpolated' } string"
# will render:
<span>I am a interpolated string</span>
In your case:
%param{:name => "flashvars", :value => "controls=true&file=#{#video_resource.file_path}"}
^^ ^

Related

I want to use Jalali date in my Laravel / Angular database but I faced with this error: Whoops, looks like something went wrong

Recently I have purchased "Schoex - Ultimate school management system", a software for managing school. This software designed by Laravel 4 and AngularJS. Default date for this software is Gerigorian and I want to change it to Jalali date. I found different package from internet but not any one worked.
For example I tried to used this package: https://github.com/morilog/jalali
and It shows this error: Whoops, looks like something went wrong.
app.php
<?php
return array(
'debug' => false,
'log' => 'daily',
'url' => '',
'timezone' => 'Asia/Kabul',
'locale' => 'fa',
'key' => 'SchoexSchoolApplicationByCrHouse',
'providers' => array(
'Illuminate\Foundation\Providers\ArtisanServiceProvider',
'Illuminate\Auth\AuthServiceProvider',
'Illuminate\Cache\CacheServiceProvider',
'Illuminate\Session\CommandsServiceProvider',
'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
'Illuminate\Routing\ControllerServiceProvider',
'Illuminate\Cookie\CookieServiceProvider',
'Illuminate\Database\DatabaseServiceProvider',
'Illuminate\Encryption\EncryptionServiceProvider',
'Illuminate\Filesystem\FilesystemServiceProvider',
'Illuminate\Hashing\HashServiceProvider',
'Illuminate\Html\HtmlServiceProvider',
'Illuminate\Log\LogServiceProvider',
'Illuminate\Mail\MailServiceProvider',
'Illuminate\Database\MigrationServiceProvider',
'Illuminate\Pagination\PaginationServiceProvider',
'Illuminate\Queue\QueueServiceProvider',
'Illuminate\Redis\RedisServiceProvider',
'Illuminate\Remote\RemoteServiceProvider',
'Illuminate\Auth\Reminders\ReminderServiceProvider',
'Illuminate\Database\SeedServiceProvider',
'Illuminate\Session\SessionServiceProvider',
'Illuminate\Translation\TranslationServiceProvider',
'Illuminate\Validation\ValidationServiceProvider',
'Illuminate\View\ViewServiceProvider',
'Illuminate\Workbench\WorkbenchServiceProvider',
'Tappleby\AuthToken\AuthTokenServiceProvider',
'Morilog\JalaliServiceProvider',
),
'manifest' => storage_path().'/meta',
'aliases' => array(
'App' => 'Illuminate\Support\Facades\App',
'Artisan' => 'Illuminate\Support\Facades\Artisan',
'Auth' => 'Illuminate\Support\Facades\Auth',
'Blade' => 'Illuminate\Support\Facades\Blade',
'Cache' => 'Illuminate\Support\Facades\Cache',
'ClassLoader' => 'Illuminate\Support\ClassLoader',
'Config' => 'Illuminate\Support\Facades\Config',
'Controller' => 'Illuminate\Routing\Controller',
'Cookie' => 'Illuminate\Support\Facades\Cookie',
'Crypt' => 'Illuminate\Support\Facades\Crypt',
'DB' => 'Illuminate\Support\Facades\DB',
'Eloquent' => 'Illuminate\Database\Eloquent\Model',
'Event' => 'Illuminate\Support\Facades\Event',
'File' => 'Illuminate\Support\Facades\File',
'Form' => 'Illuminate\Support\Facades\Form',
'Hash' => 'Illuminate\Support\Facades\Hash',
'HTML' => 'Illuminate\Support\Facades\HTML',
'Input' => 'Illuminate\Support\Facades\Input',
'Lang' => 'Illuminate\Support\Facades\Lang',
'Log' => 'Illuminate\Support\Facades\Log',
'Mail' => 'Illuminate\Support\Facades\Mail',
'Paginator' => 'Illuminate\Support\Facades\Paginator',
'Password' => 'Illuminate\Support\Facades\Password',
'Queue' => 'Illuminate\Support\Facades\Queue',
'Redirect' => 'Illuminate\Support\Facades\Redirect',
'Redis' => 'Illuminate\Support\Facades\Redis',
'Request' => 'Illuminate\Support\Facades\Request',
'Response' => 'Illuminate\Support\Facades\Response',
'Route' => 'Illuminate\Support\Facades\Route',
'Schema' => 'Illuminate\Support\Facades\Schema',
'Seeder' => 'Illuminate\Database\Seeder',
'Session' => 'Illuminate\Support\Facades\Session',
'SSH' => 'Illuminate\Support\Facades\SSH',
'Str' => 'Illuminate\Support\Str',
'URL' => 'Illuminate\Support\Facades\URL',
'Validator' => 'Illuminate\Support\Facades\Validator',
'View' => 'Illuminate\Support\Facades\View',
'AuthToken' => 'Tappleby\Support\Facades\AuthToken',
'AuthTokenNotAuthorizedException' => 'Tappleby\AuthToken\Exceptions\NotAuthorizedException',
'jDate' => 'Morilog\Facades\jDate',
'jDateTime' => 'Morilog\Facades\jDateTime',
),
);

ZF2 - \Zend\Db\Sql\Expression and DATE_FORMAT

I'm using Zend Framework 2.4 and trying to amend a working SQL columns select from
'content_publish_date' => 'publish_date',
to
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(\'content.publish_date\',\'%d/%c/%Y\')'),
produces the following SQL...
DATE_FORMAT('content.publish_date','%%d/%%c/%%Y') AS `content_publish_date`,
... which fails syntactically on MySQL (note the extra '%')
I have tried various alternatives...
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(content.publish_date,\'%d/%c/%Y\')'),
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(content.publish_date,"%d/%c/%Y")'),
... and so on, none of which has helped.
Does anyone have any idea how to use the Expression correctly to get the correct generated SQL?
The full code for this piece is as follows:
$dbSelect->join(
'content',
'content.id = category.content_id',
array(
'content_title' => 'title',
'content_html_title' => 'html_title',
'content_description' => 'description',
'content_content_text' => 'content_text',
'content_url' => 'url',
'content_url_vars' => 'url_vars',
'content_meta_keyword' => 'meta_keyword',
'content_meta_description' => 'meta_description',
'content_content_type_id' => 'content_type_id',
'content_disclaimer_id' => 'disclaimer_id',
'content_agent_target_id' => 'agent_target_id',
'content_video_position' => 'video_position',
'content_video_size' => 'video_size',
'content_creation_date' => 'creation_date',
'content_publish_date' => new \Zend\Db\Sql\Expression('DATE_FORMAT(\'content.publish_date\',\'%d/%c/%Y\')'),
'content_flag_content_url' => 'flag_content_url',
'content_flags' => 'flags'
),
\Zend\Db\Sql\Select::JOIN_LEFT
);
Thanks in advance.

Ruby: Cleanup my code w/ stored procedure?

this is my first ruby script. I'd like to clean it up. How would I go about storing all of the following nessus_scans.insert() arguments into a variable to reduce duplicate lines in my code? I'd like to store :Id => "", :scan_title => "" etc all into this variable and then just do something like nessus_scans.insert(myvariable). This query is used about 5 times in the code which is why I want to simplify it. Thanks!
nessus_scans.insert(:Id => "",
:scan_title => "#{scan.title}",
:hostname => "#{host.hostname}",
:host_ip => "#{host.ip}",
:mac_addr => "#{host.mac_addr}",
:netbios_name => "#{host.netbios_name}",
:open_ports => "#{host.ports.map(&:inspect).join(', ').tr('"', '')}",
:operating_system => "#{host.operating_system}",
:start_time => "#{host.start_time}",
:stop_time => "#{host.stop_time}",
:runtime => "#{host.runtime}",
:cve => "#{event.cve}",
:cvss_base_Score => "#{event.cvss_base_score}",
:description => "#{event.description}",
:family => "#{event.family}",
:plugin_id => "#{event.plugin_id}",
:output => "#{event.output}",
:event_name => "#{event.name}",
:patch_publication_date => "#{event.patch_publication_date}",
:plugin_version => "#{event.plugin_version}",
:event_port => "#{event.port.number}",
:risk => "#{event.risk}",
:see_also => "#{event.see_also}",
:severity => "#{event.severity.in_words}",
:solution => "#{event.solution}",
:synopsis => "#{event.synopsis}",
:xref => "#{event.xref}",
:bool_crit => "#{event.critical?}",
:bool_high => "#{event.high?}",
:bool_med => "#{event.medium?}",
:bool_low => "#{event.low?}",
:bool_info => "#{event.informational?}")
end # end event.informational?
my_data = {
Id: "",
scan_title: scan.title,
hostname: host.hostname,
...
}
nessus_scans.insert(my_data)
These are all equivalent:
def method(arg1, arg2)
p arg1, arg2
end
method(10, a: 20, b:30)
--output:--
10
{:a=>20, :b=>30}
method(10, {a: 20, b:30})
--output:--
10
{:a=>20, :b=>30}
data = {a: 20, b:30}
number = 10
method 10, data
--output:--
10
{:a=>20, :b=>30}

How to cache database translations on yii2

How to cache database translations on yii2
I tried the following but not worked
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache'
],
The problem is in Zelenin i18n module. If you look at Module.php file, you can see:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'on missingTranslation' => $this->missingTranslationHandler
];
in init() method. This Code set DbMessageSource options and there are no any options about caching. Module have no any caching options too.
If you change this code to:
$this->translations['*'] = [
'class' => DbMessageSource::className(),
'sourceMessageTable' => $this->sourceMessageTable,
'messageTable' => $this->messageTable,
'enableCaching' => true,
'cachingDuration' => 3600,
'on missingTranslation' => $this->missingTranslationHandler
];
Cache will work. Some SELECT messages will gone from debug list.
The Yii documentation for i18n db messages says that the property cache only has meaning when the property cacheDuration is not zero. I suggest you set this value, so;
'i18n' => [
'class' => Zelenin\yii\modules\I18n\components\I18N::className(),
'languages' => ['en', 'ar', 'fr'],
'sourceMessageTable' => 'source_message',
'messageTable' => 'message',
'cache' => 'cache',
'cacheDuration' => 3600
],

ThinkingSphinx: Run SQL String Through Association

I'm trying to get a geo search to work via an association. Very similar to this fellow:
How do I geo-search multiple models with ThinkingSphinx?
The one difference is that I'm trying to combine the association syntax with custom SQL syntax. It doesn't print any errors on indexing, but when I try to search it fails:
class Person
define_index do
indexes tags(:text), :as => :tags
has media.location.lat('RADIANS(location.lat)'), :as => :lat, :type => :float
has media.location.lng('RADIANS(location.lng)'), :as => :lng, :type => :float
end
sphinx_scope(:by_location) { |loc|
{ :geo => [loc.lat.to_radians, loc.lng.to_radians],
:with => {"#geodist" => 0.0..loc.radius },
:latitude_attr => "lat",
:longitude_attr => "lng"
}
}
end
#running this search from console
Person.by_location(Location.first)
This is the error:
ThinkingSphinx::SphinxError: index fulfillment_core: unknown latitude attribute 'lat'
I've tried configuring it without the SQL string - this runs without error, but the math is of course totally wrong as it's trying to do radian operations on degrees.
Is there any way to combine the conversion and the association, or am I stuck storing my data as radians instead of degrees?
Your index definition isn't quite right. Try the following:
define_index do
indexes tags(:text), :as => :tags
# forces the join on media and location associations
join media.location
has 'RADIANS(location.lat)', :as => :lat, :type => :float
has 'RADIANS(location.lng)', :as => :lng, :type => :float
end