'
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 m(*args)
_(*args) { |t|
markdown(t).html_safe
}
end
def markdown(object, attribute = nil)
return '' unless object
content = attribute ? object.send(attribute.to_s) : object
@markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML.new({
filter_html: true,
hard_wrap: true,
space_after_headers: true,
fenced_code_blocks: true,
link_attributes: { target: "_blank" }
}), {
autolink: true,
disable_indented_code_blocks: true,
superscript: true
})
@markdown.render(content)
end
def paragraph(object, attribute = nil)
return '' unless object
content = attribute ? object.send(attribute.to_s) : object
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 = markdown(object, attribute)
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' || request.remote_ip == '::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 get_remote_location
Geocoder.search(session['remote_ip'] || (session['remote_ip'] = open("http://checkip.dyndns.org").first.gsub(/^.*\s([\d\.]+).*$/s, '\1').gsub(/[^\.\d]/, ''))).first
end
def lookup_ip_location
begin
if is_test? && ApplicationController::get_location.present?
Geocoder.search(ApplicationController::get_location).first
elsif request.remote_ip == '127.0.0.1' || request.remote_ip == '::1'
get_remote_location
else
request.location || get_remote_location
end
rescue
nil
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' || Rails.env == 'preview'
end
def is_test?
Rails.env == 'test'
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
def rand_hash(length = 16, model = nil, field = nil)
if field
hash = rand_hash(length)
while !model.to_s.to_s.singularize.classify.constantize.find_by(field => hash).nil?
hash = rand_hash(length)
end
end
rand(36**length).to_s(36)
end
def get_panoramio_image(location)
if is_test?
params[:image] = 'panoramio.jpg'
params[:attribution_id] = 1234
params[:attribution_user_id] = 5678
params[:attribution_name] = 'Some Guy'
params[:attribution_src] = 'panoramio'
return params
end
location = location.city + ', ' + (location.territory ? location.territory + ' ' : '') + location.country
$panoramios ||= Hash.new
$panoramios[location] ||= 0
$panoramios[location] += 1
result = Geocoder.search(location).first
if result
points = Geocoder::Calculations.bounding_box([result.latitude, result.longitude], 5, { :unit => :km })
options = {:set => :public, :size => :original, :from => 0, :to => 20, :mapfilter => false, :miny => points[0], :minx => points[1], :maxy => points[2], :maxx => points[3]}
url = 'http://www.panoramio.com/map/get_panoramas.php?' + options.to_query
response = JSON.parse(open(url).read)
response['photos'].each { |img|
if img['width'].to_i > 980
if Organization.find_by(:cover_attribution_id => img['photo_id'], :cover_attribution_src => 'panoramio').nil? && Conference.find_by(:cover_attribution_id => img['photo_id'], :cover_attribution_src => 'panoramio').nil?
params[:image] = img['photo_file_url']
params[:attribution_id] = img['photo_id']
params[:attribution_user_id] = img['owner_id']
params[:attribution_name] = img['owner_name']
params[:attribution_src] = 'panoramio'
return params
end
end
}
end
return nil
end
def get_secure_info(name)
YAML.load(File.read(Rails.root.join("config/#{name.to_s}.yml")))[Rails.env].symbolize_keys
end
def location(location)
return nil if location.blank?
city = nil
region = nil
country = nil
if location.is_a?(Location)
country = location.country
region = location.territory
city = location.city
else
country = location.data['country_code']
region = location.data['region_code']
city = location.data['city']
end
l = Array.new
l << (_!city)
l << I18n.t("geography.subregions.#{country}.#{region}") if region.present?
l << I18n.t("geography.countries.#{country}") if !(country =~ /^(US|CA)$/)
l.join(_!', ')
end
def nav_link(link, title)
link_to "#{title}".html_safe, link, :class => (current_page?(link) ? 'current' : nil)
end
def date(date, format = :long)
I18n.l(date, :format => format) # default, long, short
end
def date_span(date1, date2)
key = 'same_month'
if date1.year != date2.year
key = 'different_year'
elsif date1.month != date2.month
key = 'same_year'
end
d1 = I18n.l(date1.to_date, format: "span_#{key}_date_1".to_sym)
d2 = I18n.l(date2.to_date, format: "span_#{key}_date_2".to_sym)
I18n.t('date.date_span', {:date_1 => d1, :date_2 => d2})
end
def generate_confirmation(user, url, expiry = nil)
ApplicationController::generate_confirmation(user, url, expiry)
end
def money(amount)
return _!('$0.00') if amount == 0
_!((amount * 100).to_i.to_s.gsub(/^(.*)(\d\d)$/, '$\1.\2'))
end
def percent(p)
return _!('0.00%') if p == 0
_!((p * 10000).to_i.to_s.gsub(/^(.*)(\d\d)$/, '\1.\2%'))
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