'
objects.each_index do |i|
@this = objects[i]
result += '
'
result += hidden_field_tag (id + "[#{i}]"), objects[i][id]
result += hidden_field_tag ('position' + "[#{i}]"), i, :class => 'sortable-position'
if block_given?
result += capture(objects[i], &block)
end
result += '
'
end
result += ''
result.html_safe
end
def tabs object, tabs
type = object.class.name.downcase
tab_list = ''
tabs.each do |tab|
link = nil
if self.respond_to?(type + '_' + tab.to_s + '_path')
link = self.send(type + '_' + tab.to_s + '_path', object)
elsif self.respond_to?(tab.to_s + '_' + type + '_path')
link = self.send(tab.to_s + '_' + type + '_path', object)
end
c = ['tab', 'tab-' + (link ? tab.to_s : 'show')]
if params[:action] == tab.to_s
c << 'current'
end
link_html = ''
if tab.is_a?(Hash)
func = tab.keys[0]
val = tab[func]
args = val ? (val.is_a?(Array) ? (val.collect { |v| object[v] } ) : [object[val]] ) : nil
link_html = link_to func.to_s.gsub(/_path$/, ''), args ? self.send(func, args) : self.send(func), :class => c
else
#x
#link_html = link_to tab, link || object, :class => c
end
tab_list += link_html
end
('').html_safe
end
def tabs!
object = nil
tabs = nil
case params[:controller]
when 'organizations'
object = @organization
tabs = OrganizationsHelper::TABS
when 'conferences'
object = @conference
tabs = ConferencesHelper::TABS
when 'workshops'
object = [@conference, @workshop]
tabs = WorkshopsHelper::TABS
end
if object && tabs
return tabs object, tabs
end
end
def sub_tabs object, tabs
type = object.class.name.downcase
tab_list = ''
tabs.each do |tab|
link = nil
if self.respond_to?(type + '_' + tab.to_s + '_path')
link = self.send(type + '_' + tab.to_s + '_path', object)
elsif self.respond_to?(tab.to_s + '_' + type + '_path')
link = self.send(tab.to_s + '_' + type + '_path', object)
end
c = ['sub-tab', 'sub-tab-' + (link ? tab.to_s : 'show')]
if current_page?(link)
c << 'current'
end
tab_list += link_to tab, link || object, :class => c
end
('').html_safe
end
def sub_tabs!
object = nil
tabs = nil
case params[:controller]
when 'organizations'
object = @organization
tabs = OrganizationsHelper::SUB_TABS
when 'conferences'
object = @conference
tabs = ConferencesHelper::SUB_TABS
end
if object && tabs
return sub_tabs object, tabs
end
end
def p(object, attribute)
content = object.send(attribute.to_s)
result = ''
if content =~ /<(p|span|h\d|div)[^>]*>/
result = content.gsub(/\s*(style|class|id|width|height|font)=\".*?\"/, '')
.gsub(/ /, ' ')
.gsub(/<(\/)?\s*h\d\s*>/, '<\1h3>')
.gsub(/
(.*?) \s*( )+/, '
\1
')
.gsub(/]*>\s*(.*?)\s*<\/span>/, '\1')
.gsub(/
\s*<\/p>/, '')
.gsub(/<(\/)?div>/, '<\1p>')
if !(result =~ /
]*>/)
result = '
' + result + '
'
end
else
result = '
' + content.strip.gsub(/\s*\n+\s*/, '
') + '
'
end
result.html_safe
end
def form_field(f, response = nil)
id = 'field_' + f.id.to_s
html = p(f, 'title')
options = JSON.parse(f.options)
if f.field_type == 'multiple'
if f.help
html += ('
' + p(f, 'help') + '
').html_safe
end
opts = Hash.new
options['options'].split(/\s*\n\s*/).each do |value|
kv = value.split(/\s*\|\s*/, 2)
opts[kv[0]] = kv[1]
end
val = response ? ActiveSupport::JSON.decode(response.data) : Hash.new
if f.repeats?
is_array = f.is_array?
opts.each do |key, value|
n = (id + (is_array ? ('_' + key) : '')).to_sym
v = is_array ? (val ? val[key] : nil) : key
o = {:label => value}
if f.required
options[:required] = true
end
html += _form_field(options['selection_type'], n, v, o)
end
else
html += _form_field(options['selection_type'], id.to_sym, options_for_select(opts.invert, val), {})
end
else
#html += field(id.to_sym, options['input_type'] + '_tag', label: false, placeholder: f.help, value: response ? ActiveSupport::JSON.decode(response.data) : nil, required: f.required)
opts = {label: false, placeholder: f.help && f.help.length > 0 ? f.help : false}
if f.required
opts[:required] = true
end
html += _form_field(options['input_type'], id.to_sym, response ? ActiveSupport::JSON.decode(response.data) : nil, opts)
end
html.html_safe
end
def t(*a)
_(*a)
end
def lookup_ip
if request.remote_ip == '127.0.0.1'
session['remote_ip'] || (session['remote_ip'] = open("http://checkip.dyndns.org").first.gsub(/^.*\s([\d\.]+).*$/s, '\1').gsub(/[^\.\d]/, ''))
else
request.remote_ip
end
end
def lookup_ip_location
if request.remote_ip == '127.0.0.1'
Geocoder.search(session['remote_ip'] || (session['remote_ip'] = open("http://checkip.dyndns.org").first.gsub(/^.*\s([\d\.]+).*$/s, '\1').gsub(/[^\.\d]/, ''))).first
else
request.location
end
end
def hash_to_html_attributes(hash, prefix = '')
attributes = ''
if hash
hash.each { |k,v|
k = k.to_s
if v
if v.is_a?(Hash)
attributes += hash_to_html_attributes(v, 'data-')
else
attributes += " #{k}=\"" + (v.is_a?(Array) ? v.join(' ') : v) + '"'
end
end
}
end
attributes
end
def icon(id, attributes = nil)
('').html_safe
end
def static_map(location, zoom, width, height)
require 'fileutils'
local_file_name = "#{location}-#{width}x#{height}z#{zoom}.png"
file = File.join("public", "maps/#{local_file_name}")
FileUtils.mkdir_p("public/maps") unless File.directory?("public/maps")
if !File.exist?(file)
url = "https://maps.googleapis.com/maps/api/staticmap?center=#{location}&zoom=#{zoom}&size=#{width}x#{height}&maptype=roadmap&markers=size:small%7C#{location}&key=AIzaSyAH7U8xUUb8IwDPy1wWuYGprzxf4E1Jj4o"
require 'open-uri'
open(file, 'wb') do |f|
f << open(url).read
end
end
cdn("/maps/#{local_file_name}")
end
def cdn(file)
(Rails.application.config.action_controller.asset_host || '') + file
end
def is_production?
Rails.env == 'production'
end
def subdomain
request.env['SERVER_NAME'].gsub(/^(\w+)\..*$/, '\1')
end
def is_test_server?
subdomain == 'test'
end
def location(location)
territory = Carmen::Country.coded(location.country).subregions.coded(location.territory)
location.city + (territory ? ' ' + territory.name : '') + ', ' + Carmen::Country.coded(location.country).name
end
private
def _form_field(type, name, value, options)
if type == 'check_box'
self.send(type + '_tag', name, "1", value, options)
else
self.send(type + '_tag', name, value, options)
end
end
end