Ruby on Rails Formtastic Multiple Options in Member_Label - forms

I have a relational model where users have managers that are also users. The below code works great and does exactly what it's suppose to, but it's only displaying the first name of the manager. I'm trying to get this to show both the first name and the last name of the manager.
<%= sf.input :managers, :as => :check_boxes, :member_label => (:firstname) ,:input_html => { :size => 20, :multiple => true}%>
The other field i'm trying to add is the :lastname. I cannot figure out how to get :member_label to take both fields.

I figured it out. By using the Proc.new, I was able to add in both first name and last name.
<%= sf.input :managers, :as => :check_boxes, :member_label => Proc.new { |t| h(t.firstname + " " + t.lastname) } ,:input_html => { :size => 20, :multiple => true}%>

Related

Getting initial_filter to work with subpanel's Select button

In Suitecrm have a subpanel and when clicking 'Select' I want it to show only a specific set of records.
I have a subpanel definition file that looks like this and tried all possible variations in the section "initial_filter" but when the pop-up comes up it shows all of the records
$layout_defs["un_inventory"]["subpanel_setup"]['un_inventory_leads_1'] = array (
'order' => 0,
'module' => 'Leads',
'subpanel_name' => 'default',
'sort_order' => 'asc',
'sort_by' => 'id',
'title_key' => 'LBL_UN_INVENTORY_LEADS_1_FROM_LEADS_TITLE',
//'get_subpanel_data' => 'un_inventory_leads_1',
'get_subpanel_data' => 'function:get_parent_leads',
'function_parameters' =>
array('import_function_file' => 'custom/modules/Leads/func/get_parent_leads_file.php',),
'top_buttons' =>
array (
0 =>
array (
'widget_class' => 'SubPanelTopButtonQuickCreate',
),
1 =>
array (
//'widget_class' => 'SubPanelTopSelectButton',
'widget_class' => 'SubPanelTopSelectButtonParentProjectLeads',
'mode' => 'MultiSelect',
// 'initial_filter_fields' => "&first_name_advanced=hello",
// 'initial_filter' => array('parent_project_id_c_advanced' => array('83b30b20-83a6-8099-b3b9-5d4a491888e6')),
// 'initial_filter' => array('parent_project_id_c_advanced' => '83b30b20-83a6-8099-b3b9-5d4a491888e6'),
// 'initial_filter' => array('account_type_advanced' => array('Student')),
// 'initial_filter' => '&parent_project_id_c=83b30b20-83a6-8099-b3b9-5d4a491888e6',
),
),
);
There are many example of how this is done for 'relate' fields in editview, but not so much for subpanel's such as above, I'm pretty sure many would find this valuable.
Solution would probably be applicable to sugarcrm CE also
Solution is to override the default TopButtonSelect class and hardcode the $initial_filter variable.
So if we hardcode the value like so
$initial_filter.='&parent_project_id_c_advanced='.urlencode("83b30b20-83a6-8099-b3b9-5d4a491888e6");
It will only show records which have the parent_project_id_c field with the value 83b30b20-83a6-8099-b3b9-5d4a491888e6
Hope this helps
Source: http://qc-digital.com/filter-values-shown-when-we-click-on-select-button-inside-a-subpanel/
The above soluton proposed by Robert Sinclair does work, but it creates a dedicated-use situation. If instead you wanted to create a solution that will let you use the changes to have a functional initial_filter for any subpanel, make the following edits.
Thank you to Robert Sinclair and qc-digital.com for not only showing how to make his solution work but also for explaining why the expected method does not work, enabling the edits below to fix the SugarWidget and have it behave as expected.
Two fixes in this post:
a) Have the initial_filter_fields array be checked against the target
module not the host module
b) have the key=>value arrangement in the
layoutdefs work like it does in other situations
Two steps to make this fix work:
Create a custom SugarWidget
Tell the subpanel to use the custom SugarWidget and add the initial_filter_fields array to the TopSelectButton definition
You can then use the custom SugarWidget in any other subpanel and the initial_filter_field will work
Create the custom SugarWidget
mkdir -p custom/include/generic/SugarWidgets/
cp -a include/generic/SugarWidgets/SugarWidgetSubPanelTopSelectButton.php custom/include/generic/SugarWidget/SugarWidgetSubPanelTopSelectButtonWithFilter.php
nano custom/include/generic/SugarWidgets/SugarWidgetSubPanelTopSelectButtonWithFilter.php
Change
class SugarWidgetSubPanelTopSelectButton extends SugarWidgetSubPanelTopButton
To
class SugarWidgetSubPanelTopSelectButtonWithFilter extends SugarWidgetSubPanelTopButton
Change
if (isset($widget_data['initial_filter_fields'])) {
if (is_array($widget_data['initial_filter_fields'])) {
foreach ($widget_data['initial_filter_fields'] as $value=>$alias) {
if (isset($focus->$value) and !empty($focus->$value)) {
$initial_filter.="&".$alias . '='.urlencode($value);
}
}
}
}
To
/*
* Edited Below to use the target module not the host module
* and to use the normal key=>value arrangement
if (isset($widget_data['initial_filter_fields'])) {
if (is_array($widget_data['initial_filter_fields'])) {
foreach ($widget_data['initial_filter_fields'] as $value=>$alias) {
if (isset($focus->$value) and !empty($focus->$value)) {
$initial_filter.="&".$alias . '='.urlencode($value);
}
}
}
}
*/
if (isset($widget_data['initial_filter_fields'])) {
if (is_array($widget_data['initial_filter_fields'])) {
foreach ($widget_data['initial_filter_fields'] as $alias=>$value) {
$initial_filter.="&".$alias . '='.urlencode($value);
}
}
}
Tell the subpanel to use the custom SugarWidget (in my case, editing the Events module (FP_events) subpanel I created pointing to Accounts ... edit to suit your situation)
nano custom/Extension/modules/FP_events/Ext/Layoutdefs/fp_events_accounts_1_FP_events.php
Change
array (
'widget_class' => 'SubPanelTopSelectButton',
'mode' => 'MultiSelect',
),
To
array (
'widget_class' => 'SubPanelTopSelectButtonWithFilter',
'mode' => 'MultiSelect',
'initial_filter_fields' => array(
'account_type_advanced' => 'Venue',
),
),
You will now be able to use the initial_filter_fields functionality in any subpanel by specifying the custom SugarWidget and including the initial_filter_fields array definition in the TopSelectButton definition.

if else to render a different form in the view of a rails 2 app

I am trying to render one of two slightly different forms with an unless else:
unless many_items
form_tag purchases_url(:item1 => item1), :id => "id1_#{item1}", :method => "post", :class=> "order-form" do
else
form_tag purchases_url(:item1 => item1, :item2 => item2), :id => "upgrade_#{item2}", :class => "upgrade_form" do
end
rest of form
button
<%end%>
but I get an error, of course.
I don't really know haw to do this..
I used a nil to prevent from the second variable to exist
parent_order = item2.present? ? item2 : nil
form_id = gig.is_item? ? "1#{gig.id}" : "2#{gig.id}"
form_class = gig.is_item? ? "1" : "2"
form_tag purchases_url(:item1 => item1 ,:item2 => items), :id => form_id, :method => "post", :class=> form_class do
%>

Mongoid/mongodb indexing

I am implementing a ruby on rails app with mongodb/mongoid and I am bit confused about the better indexing/searching structure.
I have a field staff in a model and a staff can be either of type - production, broker, office.
Each staff is a Person. Each type can have multiple staff.
So I have two approaches:
1). Make staff as an array and store it like
[{:key => 'broker', :name => "Broker Name", :person_id => "654978"},
{:key => 'office', :name => "Office Staff 1", :person_id => "564654"},
{:key => 'office', :name => 'another office', :person_id => '79878'}]
2). Make is a Hash and store is as
{:brokers => [{:person_id =>
2134, :name => 'Broker 1'}],
:office =>> [{:person_id =>
2131, :name => 'Office 1'}, {:person_id => 1231, :name => 'Office
2'}]}
I want to index these documents and should be able to search documents like, where office = '465456'.
If you store it as a hash you need several indexes. Since you then have to index the office name for each of the keys in the hash. If you store it as an array you only need 1 index.
It looks like you really want to store multiple indices; #bjartek is right, you'll want to store these as arrays:
class Office
include Mongoid::Document
embeds_many :people, as: :staff
# unsure for polymorphic embeds; perhaps this needs 'staff.name'
index "people.name"
index "people.person_id"
index "people.key"
end
http://mongoid.org/docs/indexing.html

Load a Symfony sfWidgetFormDoctrineChoice select with a limited set of results

I am developing a form in a Symfony application where a user must indicate a country, a region and an optional island using HTML select elements.
I have three models: Country, Region and Island; and Symfony has auto-generated three widgets in the form using the sfWidgetFormDoctrineChoice widget:
...
'country_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Country'), 'add_empty' => false)),
'region_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Region'), 'add_empty' => false)),
'island_id' => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Island'), 'add_empty' => true)),
...
Since the country list is large, and so the region list, I've been thinking on filtering the options available in Region and Island according to the value selected in Country.
Doing this after the HTML document is ready is easy with jQuery's change method and a simple AJAX request. But I wonder if there's a way of doing this directly from Symfony, perhaps in form configuration, to have a default combined selection.
Any suggestions?
Thanks!
After playing around with sfDependentSelectPlugin, I ended up assigning custom queries to initialize the HTML select elements:
$countryId = $this->getObject()->getCountry()->getTable()->getDefaultCountryId();
$regionId = $this->getObject()->getRegion()->getTable()->getDefaultRegionId($countryId);
$islandId = $this->getObject()->getIsland()->getTable()->getDefaultIslandId($regionId);
$this->widgetSchema->setDefault('country_id', $countryId);
$this->setWidget('region_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Region'),
'query' => $this->getObject()->getRegion()->getTable()->getRegionsQuery($countryId),
'default' => $regionId,
)));
$this->setWidget('island_id', new sfWidgetFormDoctrineChoice(array(
'model' => $this->getRelatedModelName('Island'),
'query' => $this->getObject()->getIsland()->getTable()->getIslandsQuery($regionId),
'add_empty' => '---',
'default' => $islandId,
)));
And then updating the options available with AJAX requests using jQuery. The good thing is that the actions that handle the AJAX requests use the same query methods above to return a new set of results.

Datamapper validation errors with Sinatra

I'm trying to validate my Datamapper models and show the validation errors in my HAML template, I'm using Sinatra. I'm at loss how to save those errors into an instance variable and access them from my template. I've looked around for some documentation or tutorials explaining how to do something like this but I haven't been able to find anything.
Here you go
haml view:
%label{:for => "title"} Title
%input#title{:name => "title", :type => "text", :size => "22", :maxlength => "256", :required => true, :placeholder => "Title...", :autofocus => true, :value => #post.title}/
= field_validation(#post, :title)
helper method:
def field_validation(target, field)
"<span class=\"field-validation-error\">#{target.errors[field][0]}</span>" unless target.errors[field].empty?
end