Skip to content

Commit

Permalink
Added datetimepair input and tweaked views to show fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyken committed Aug 17, 2012
1 parent 7549790 commit 9967d36
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 15 deletions.
6 changes: 0 additions & 6 deletions app/helpers/fields_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,4 @@
#------------------------------------------------------------------------------

module FieldsHelper

# only list one field per pair.
def without_pairs(fields)
fields.reject{|field| field.pair_id.present?}
end

end
75 changes: 75 additions & 0 deletions app/inputs/datetimepair_input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Fat Free CRM
# Copyright (C) 2008-2011 by Michael Dvorkin
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#------------------------------------------------------------------------------

class DatetimepairInput < SimpleForm::Inputs::DateTimeInput
include ActionView::Helpers::TagHelper
include ActionView::Helpers::JavaScriptHelper

# Output two datetime fields: start and end
#------------------------------------------------------------------------------
def input
add_autocomplete!
out = "<br />".html_safe

field1 = CustomField.where(:name => attribute_name).first
field2 = field1.try(:paired_with)

[field1, field2].compact.each do |field|
out << ((field == field1) ? I18n.t('pair.start') : I18n.t('pair.end'))
input_options = input_html_options.merge(datetimepair_options(object, field))
text = @builder.text_field(field.name, input_options)
element_id = text[/id="([a-z0-9_]*)"/, 1]
text << javascript_tag(%Q{crm.date_select_popup('#{element_id}', false, #{!!(input_type =~ /time/)});})
out << text
end

out
end

def label_target
attribute_name
end

private

def datetimepair_options(obj, field)
value = obj.send(field.name)
input_options =
if value.present?
params = if input_type =~ /time/
[value.localtime, {:format => :mmddyyyy_hhmm}]
else
[value.to_date, {:format => :mmddyyyy}]
end
{ :value => I18n.localize(*params).html_safe }
else
{}
end
opts = field.input_options
input_options.merge!(:placeholder => opts[:placeholder])
input_options.merge!(:maxlength => opts[:input_html][:maxlength])
end

def has_required?
options[:required]
end

def add_autocomplete!
input_html_options[:autocomplete] ||= 'off'
end

end
12 changes: 10 additions & 2 deletions app/models/fields/field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class Field < ActiveRecord::Base

belongs_to :field_group
has_one :pair, :class_name => Field, :foreign_key => 'pair_id', :dependent => :destroy
scope :without_pairs, where('pair_id IS NULL')

delegate :klass, :klass_name, :klass_name=, :to => :field_group

Expand Down Expand Up @@ -76,7 +77,8 @@ class Field < ActiveRecord::Base
validates_inclusion_of :as, :in => FIELD_TYPES.keys, :message => "Invalid Field Type."

# for datepair and datetimepair, ensures 'end' is greater than 'start'
#validates_numericality_of :value, :greater_than_or_equal_to => Proc.new { |field| field.pair.value } :if => Proc.new{|field| %w(datepair datetimepair).include?(field.as) and field.pair.present?}
# how does this go on custom fields?
#validates_numericality_of :value, :greater_than_or_equal_to => Proc.new { |field| field.paired_with.try(:value) }, :if => Proc.new{|field| %w(datepair datetimepair).include?(field.as) and field.pair_id.present?}

def self.field_types
# Expands concise FIELD_TYPES into a more usable hash
Expand Down Expand Up @@ -113,7 +115,13 @@ def collection_string
end

def render_value(object)
render object.send(name)
if %w(datepair datetimepair).include?(as) and paired_with.present?
from = render object.send(name)
to = render object.send(paired_with.name)
I18n.t('pair.from_to', :from => from, :to => to)
else
render object.send(name)
end
end

def render(value)
Expand Down
1 change: 0 additions & 1 deletion app/models/fields/field_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,3 @@ def move_fields_to_default_field_group
self.reload
end
end

4 changes: 2 additions & 2 deletions app/views/admin/field_groups/_field_group.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
.remote{ hidden.merge(:id => create_form_id) }

.list{ :id => dom_id(field_group, :fields) }
- if (fields = field_group.fields).present?
= render :partial => "admin/fields/field", :collection => without_pairs(fields)
- if (fields = field_group.fields.without_pairs).present?
= render :partial => "admin/fields/field", :collection => fields
- else
%div{:id => "empty_#{asset}_field_group_#{field_group.id}"}
= t(:field_group_empty)
Expand Down
2 changes: 1 addition & 1 deletion app/views/fields/_group.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
%small{ hidden_if(!collapsed).merge(:id => "#{field_group.key}_intro") }
%div[field_group]{ hidden_if(collapsed) }
%table
- field_group.custom_fields.in_groups_of(2, false) do |group|
- field_group.custom_fields.without_pairs.in_groups_of(2, false) do |group|
%tr
- group.each_with_index do |field, i|
%td
Expand Down
3 changes: 1 addition & 2 deletions app/views/fields/_sidebar_show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
- if field_group.fields.select{|f| asset.send(f.name).present? }.any?
%div
.caption #{field_group.label_i18n}
- field_group.custom_fields.each do |field|
- field_group.custom_fields.without_pairs.each do |field|
- if (value = field.render_value(asset)).present?
== #{field.label}:<br /> <b>#{truncate(value, :length => 35)}</b><br />

3 changes: 2 additions & 1 deletion config/locales/en-US_fat_free_crm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -869,4 +869,5 @@ en-US:

pair:
start: Start
end: End
end: End
from_to: From %{from} to %{to}

0 comments on commit 9967d36

Please sign in to comment.