friendly_extensions 0.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 29c0b1955597b587688ee28aa4ed2db4aa362e21
4
+ data.tar.gz: e8877f694cd2e9f205cd0f5791ebcec25f0187d6
5
+ SHA512:
6
+ metadata.gz: 14e5181cd8bf96edb76e90108ffaf724f62037f91098cc03ed23119e13d63f07e0ca41320f7e05e513e64887516208ebaea074fba7941d13d6d0febdd6fe1d40
7
+ data.tar.gz: b10b32aa9e92e6737ff0c4635270547ecbc903b180d84896f15349c7fa4c4c68913065b62df48c0e95ca19decfb9f44a5f0e4cb84e430f827f721a0c59d68508
@@ -0,0 +1,27 @@
1
+ module FriendsFormsHelper
2
+
3
+ def fieldset(title, options = {}, &block)
4
+ if options[:if].nil? || options[:if] == true
5
+ data_string = " "
6
+ if options[:data]
7
+ options[:data].each {|d,v| data_string << "#{d}='v' "}
8
+ end
9
+ concat raw("<fieldset id='#{options[:id]}' #{data_string} class='shadow #{options[:mainclass]}' #{("style='height: %spx'" % options[:height]) if options[:height]} style='#{options[:mainstyle]}'>")
10
+ concat raw("<legend class='#{options[:class]}' style='#{options[:style]}'>#{title}</legend><div class='fieldset-content'>")
11
+ yield
12
+ concat raw("</div></fieldset>")
13
+ end
14
+ end
15
+
16
+ def tooltip_box(tooltip)
17
+ "<span class='label-tooltip' title='#{tooltip}'>&nbsp;</span>".html_safe
18
+ end
19
+
20
+ def tooltip_content_box(name, options = {}, &block)
21
+ options[:default_class] ||= "icon-clue"
22
+ html = "<div class='tooltip-box'><div class='tooltip-box-label #{options[:default_class]}'>"
23
+ html << "#{name}<div class='tooltip-box-content #{options[:css]} box rounded shadow'>#{capture(&block)}</div></div></div>"
24
+ concat(html.html_safe)
25
+ end
26
+
27
+ end
@@ -0,0 +1,124 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module FriendsLabeledFormHelper
3
+
4
+ def self.included(arg)
5
+ ActionView::Helpers::FormBuilder.send(:include, FriendsLabeledFormBuilder)
6
+ end
7
+
8
+ def label_tag_for(object_name, method, options = {})
9
+ label_name = (options[:label].blank? ? method.to_label : options[:label])
10
+
11
+ label_name += options[:append] unless options[:append].nil?
12
+
13
+ label_text = "<span class='label-text'>#{label_name}</span>"
14
+
15
+ if options[:errors]
16
+ label_text << tooltip_box(options[:errors].to_text)
17
+ elsif ((options[:label].blank? && options[:tooltip].blank?) || !options[:tooltip].blank?) && options[:tooltip] != false
18
+ tooltip = (options[:tooltip] || method.to_label(:show_tooltip => true))
19
+ if !tooltip.blank? && options[:hide_tooltip] != true
20
+ # Setting Tooltip format for display in Browsertooltip
21
+ # line breaks in unicode
22
+ tooltip = tooltip.gsub(/\n|\<br(\ )?(\/)?\>/, "&#xA;")
23
+ # single to double quotes
24
+ tooltip = tooltip.gsub("'", '"')
25
+ label_text << tooltip_box(tooltip)
26
+ end
27
+ end
28
+
29
+
30
+ label_options = {}
31
+ label_options.merge!(:class => options[:class]) unless options[:class].blank?
32
+ label_options.merge!(:for => options[:for]) unless options[:for].blank?
33
+ label_options.merge!(:id => options[:id]) unless options[:id].blank?
34
+
35
+ return label(object_name, method, label_text.html_safe, label_options).html_safe
36
+ end
37
+
38
+ def labeled_text_field(object_name, method, options={})
39
+
40
+ if options[:id]
41
+ label_options = options.merge(:id => "label-#{options[:id]}")
42
+ else
43
+ label_options = options
44
+ end
45
+
46
+ html = label_tag_for(object_name, method, label_options)
47
+ if options[:password] && options.delete(:password)
48
+ html += password_field(object_name, method, options)
49
+ else
50
+ html += text_field(object_name, method, options)
51
+ end
52
+ return html.html_safe
53
+ end
54
+
55
+ def labeled_check_box(object_name, method, options={}, checked_value=1, unchecked_value=0)
56
+ method = method.to_s.to_sym
57
+ box_id = object_name.to_s+"_"+method.to_s
58
+
59
+ html = "<div class='cf labeled-check-box'>"
60
+ html += "<div class='checkbox-container'>#{check_box(object_name, method, options.merge!(:id => box_id), checked_value, unchecked_value)}</div>"
61
+ html += "<div class='checkbox-label-container'>#{label_tag_for(object_name, method, :label => options[:label], :tooltip => options[:tooltip], :class => "checkbox-label", :for => box_id, :errors => options[:errors])}</div>"
62
+ html += "</div>"
63
+
64
+ return html.html_safe
65
+ end
66
+
67
+
68
+
69
+ def labeled_radio_button(object_name, method, value, options = {})
70
+ options[:id] ||= "radio-#{method.to_s}-#{value.inspect}"
71
+ options[:label] ||= method.to_label
72
+ html = "<div class='cf labeled-radio-button'>"
73
+ html << radio_button(object_name, method, value, options.except(:label, :errors))
74
+ html << "&nbsp;"
75
+ html << label_tag_for(object_name, method, {:label => options[:label], :for => options[:id]})
76
+ html << "</div>"
77
+ return raw(html)
78
+ end
79
+
80
+ end
81
+
82
+ module FriendsLabeledFormBuilder
83
+
84
+ def label_tag_for(method, options = {})
85
+ @template.label_tag_for(@object_name, method, options)
86
+ end
87
+
88
+
89
+ def labeled_radio_button(method, value, options ={})
90
+ options[:checked] = (object.send(method) == value) if options[:checked].nil?
91
+
92
+ options[:class] ||= ""
93
+ options[:class] << " error" if ((!object.errors[method.to_sym].empty? && !object.new_record?) rescue false )
94
+ options[:errors] = ((object.errors[method.to_sym].empty? ? nil : object.errors[method.to_sym]) rescue false )
95
+
96
+ @template.labeled_radio_button(@object_name, method, value, options)
97
+ end
98
+
99
+
100
+ def labeled_check_box(method, options = {}, checked_value=1, unchecked_value=0)
101
+
102
+ options[:class] ||= ""
103
+ options[:class] << " error" if ((!object.errors[method.to_sym].empty? && !object.new_record?) rescue false )
104
+ options[:errors] = ((object.errors[method.to_sym].empty? ? nil : object.errors[method.to_sym]) rescue false )
105
+
106
+ options[:checked] ||= ((object.send(method) rescue false) || options[:checked])
107
+ @template.labeled_check_box(@object_name, method, options, checked_value, unchecked_value)
108
+ end
109
+
110
+
111
+ def labeled_text_field(method, options = {})
112
+
113
+ options[:value] ||= (object.send(method) rescue nil )
114
+ options[:class] ||= ""
115
+ options[:class] << " error" if ((!object.errors[method.to_sym].empty? && !object.new_record?) rescue false )
116
+
117
+ options[:errors] = ((object.errors[method.to_sym].empty? ? nil : object.errors[method.to_sym]) rescue false )
118
+
119
+ @template.labeled_text_field(@object_name, method, options)
120
+ end
121
+
122
+ end
123
+
124
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class FriendsLabel < ActiveRecord::Base
3
+
4
+ validates_uniqueness_of :attribute_name
5
+
6
+
7
+
8
+ labels = {}
9
+
10
+ if self.table_exists?
11
+ concept_labels = FriendsLabel.all
12
+ concept_labels.each {|l| labels.merge!(l.attribute_name => {:label => l.label, :tooltip => l.tooltip, :docu_tooltip => l.tooltip_docu})}
13
+ end
14
+
15
+ LABELS = labels
16
+
17
+
18
+ def is_unset?
19
+ return (!(self.attribute_name == self.label)).to_s
20
+ end
21
+
22
+ end
@@ -0,0 +1,24 @@
1
+ states = [
2
+ "Baden-Württemberg" ,
3
+ "Bayern" ,
4
+ "Berlin " ,
5
+ "Brandenburg" ,
6
+ "Bremen " ,
7
+ "Hamburg " ,
8
+ "Hessen" ,
9
+ "Mecklenburg-Vorpommern",
10
+ "Niedersachsen" ,
11
+ "Nordrhein-Westfalen" ,
12
+ "Rheinland-Pfalz" ,
13
+ "Saarland" ,
14
+ "Sachsen" ,
15
+ "Sachsen-Anhalt" ,
16
+ "Schleswig-Holstein" ,
17
+ "Thüringen"
18
+ ]
19
+
20
+ GERMAN_STATES = states.map {|s| [s, states.index(s)+1]}
21
+ german_states_hash = {}
22
+ GERMAN_STATES.each {|s| german_states_hash.merge!(s[1] => s[0])}
23
+
24
+ GERMAN_STATES_HASH = german_states_hash
@@ -0,0 +1,13 @@
1
+ class CreateLabels < ActiveRecord::Migration
2
+
3
+ def change
4
+ create_table "friends_labels", :force => true do |t|
5
+ t.string "attribute_name"
6
+ t.string "label"
7
+ t.text "tooltip"
8
+ t.text "tooltip_docu"
9
+ t.text "search_tags"
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,76 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Alphanumeric
3
+
4
+ # Give every year a letter
5
+ # TODO: should be automatically computed
6
+ LETTERS = "abcdefghijklmnopqrstuvwxyz".upcase.split('')
7
+
8
+ START_YEAR = 1960
9
+
10
+ YEAR_LETTERS = {
11
+ 2009 => "A",
12
+ 2010 => "B",
13
+ 2011 => "C",
14
+ 2012 => "D",
15
+ 2013 => "E",
16
+ 2014 => "F",
17
+ 2015 => "G",
18
+ 2016 => "H",
19
+ 2017 => "I",
20
+ 2018 => "J",
21
+ 2019 => "K",
22
+ 2020 => "L",
23
+ 2021 => "M",
24
+ 2022 => "N",
25
+ 2023 => "O",
26
+ 2024 => "P",
27
+ 2025 => "Q",
28
+ 2026 => "R",
29
+ 2027 => "S",
30
+ 2028 => "T",
31
+ 2029 => "U",
32
+ 2030 => "V",
33
+ 2031 => "W",
34
+ 2032 => "X",
35
+ 2033 => "Y",
36
+ 2034 => "Z",
37
+ 2035 => "AA",
38
+ 2036 => "BA",
39
+ 2037 => "CA",
40
+ 2038 => "DA",
41
+ 2039 => "EA",
42
+ 2040 => "FA",
43
+ 2041 => "GA",
44
+ 2042 => "HA",
45
+ 2043 => "IA",
46
+ 2044 => "JA",
47
+ 2045 => "KA",
48
+ 2046 => "LA",
49
+ 2047 => "MA",
50
+ 2048 => "NA",
51
+ 2049 => "OA",
52
+ 2050 => "PA",
53
+ }
54
+
55
+ def self.year_letter(year=Time.now.year, letters = LETTERS)
56
+
57
+ steps = []
58
+
59
+ d = (year - START_YEAR)
60
+ n = d.fdiv(letters.size).ceil
61
+ string = ""
62
+
63
+ n.times do |i|
64
+ # First
65
+ if i == 0
66
+ string << letters[d%letters.size]
67
+ else
68
+ x = d.fdiv(letters.size*(i+1)).round
69
+ string << letters[x]
70
+ end
71
+ end
72
+
73
+ return string
74
+ end
75
+
76
+ end
data/lib/array.rb ADDED
@@ -0,0 +1,280 @@
1
+ # -*- encoding : utf-8 -*-
2
+ # Add the methods to the Array Class
3
+ module ArrayExt
4
+ Array.class_eval do
5
+
6
+ # Use sum from Activesupport
7
+ # Sum is not a default in Ruby
8
+ def sum(identity = 0, &block)
9
+ if block_given?
10
+ map(&block).sum(identity)
11
+ else
12
+ inject { |sum, element| sum + element } || identity
13
+ end
14
+ end
15
+
16
+ # Get avg. value from, either from numeric values or from
17
+ # values from a object included in array
18
+ def avg(attribute = nil)
19
+ value = self
20
+ if attribute.nil?
21
+ value.delete(nil)
22
+ value = self.map {|r| r.to_f }
23
+ else
24
+ value = self
25
+ # Bei fehlerhaften attribute wird nil gespeichert
26
+ value = value.map {|r| r.send(attribute.to_sym).to_f rescue nil}
27
+ # Leere Werte löschen
28
+ value.delete(nil)
29
+ end
30
+ # Divide by zero ausschließen
31
+ value.size == 0 ? size = 1 : size = value.size
32
+ value.sum / size
33
+ end
34
+
35
+
36
+
37
+ # Generate a hash with the given array elements as keys and 'init_value' as value
38
+ def array_to_hash(init_val = 0)
39
+ h = {}
40
+ self.each {|x| h.merge!(x => init_val) unless x.nil? }
41
+ return h
42
+ end
43
+
44
+
45
+ def to_structured_hash(attribute, options = {})
46
+ data = {}
47
+
48
+ self.each do |item|
49
+ if attribute.is_a?(Symbol) && options[:load_from_hash] != true
50
+ key = item.send(attribute) rescue ""
51
+ elsif attribute.is_a?(Symbol) && options[:load_from_hash] == true
52
+ key = item[attribute] rescue ""
53
+ elsif attribute.is_a?(String)
54
+ attribute_methods = attribute.split(".")
55
+ key = item.send(attribute_methods[0])
56
+ attribute_methods[1..-1].each do |x|
57
+ key = key.send(x) rescue ""
58
+ end
59
+ end
60
+
61
+ if data[key].nil?
62
+ data.merge!(key => [item])
63
+ else
64
+ data[key] << item
65
+ end
66
+ end
67
+
68
+ return data
69
+ end
70
+
71
+
72
+ # Sum up an array of objects with given attirbute
73
+ # Attributes can be given as symbol (:my_value will return obj.my_value)
74
+ # or as string ("user.my_value" will return my_value from user object which belongs to object in array)
75
+ # also, options could be passed, which could be *args or options hash
76
+ def sum_with_attribute(arg, opt =nil)
77
+ if arg.nil?
78
+ return self.compact.sum
79
+ else
80
+ values = []
81
+ if arg.is_a?(Symbol)
82
+ self.map {|i| ((opt.nil? ? i.send(arg) : i.send(arg, opt)) rescue 0) }.sum
83
+ elsif arg.is_a?(String)
84
+ self.each do |v|
85
+ tag_methods = arg.split(".")
86
+ tagx = v.send(tag_methods[0])
87
+ tag_methods[1..-1].each do |x|
88
+ # Use options if last method in chain called
89
+ if tag_methods[1..-1].last == x && !opt.nil?
90
+ tagx = tagx.send(x,opt)
91
+ else
92
+ tagx = tagx.send(x)
93
+ end
94
+ end
95
+ values << tagx
96
+ end
97
+ return values.compact.sum
98
+ end
99
+ end
100
+ end
101
+
102
+
103
+ # check the number of items included in the array
104
+ def count_for(item)
105
+ count = 0
106
+ self.each {|x| count += 1 if x == item}
107
+ return count
108
+ end
109
+
110
+
111
+ # return the item after the given val in array
112
+ # returns val if no item found or val is not included
113
+ # toggle :cycle => true to continue search at beginn of array if end is reached
114
+ def next(val, options = {})
115
+ i = self.index(val)
116
+ return val if i.nil?
117
+
118
+ i == self.size-1 ?
119
+ (options[:cycle] == true ? self.first : val) : self[i+1]
120
+ end
121
+
122
+
123
+ # Schnittmenge / Intersection zwischen 2 Arrays
124
+ def isec(b)
125
+ raise ArgumentError, "#{b.inspect} is not an array" if !b.is_a?(Array)
126
+ (self- (self-b))
127
+ end
128
+
129
+ # like except for hash
130
+ def except(*args)
131
+ excluded = *args
132
+ return self - excluded
133
+ end
134
+
135
+ # break array into n arrays
136
+ def seperate(n = 8)
137
+ f = n
138
+ aks_size = self.size
139
+ rest = aks_size % f
140
+
141
+ stack = ((aks_size - rest) / f)
142
+
143
+ arrays = (1..f).to_a
144
+
145
+ arrays.map! {|i| self.first(stack*i).last(stack) }
146
+ arrays[-1] += self.last(rest) if rest != 0
147
+ #arrays.last.pop if (arrays.last.last.empty? || arrays.last.last.blank?)
148
+ return arrays
149
+ end
150
+
151
+ # [1,2,3,5,6,7].stack(2)
152
+ # will return [[1, 2], [3, 5], [6, 7]]
153
+ def stack(n = 4)
154
+ arr = []
155
+ i = (self.size.to_f/n).ceil
156
+ i.times do |x|
157
+ arr << self[x*n..(((x+1)*n)-1)]
158
+ end
159
+ return arr
160
+ end
161
+
162
+
163
+ # Prepare array for use with mysql IN operator
164
+ def to_sql
165
+ return "(NULL)" if self.empty?
166
+ if self.first.is_a?(Numeric)
167
+ return "(#{self.compact.join(',')})"
168
+ else
169
+ return "(#{self.map{|i| "'#{i.to_s}'"}.join(',')})"
170
+ end
171
+ end
172
+
173
+ #== Untested / deprecated functions below, should be moved somewhere else!
174
+ # Ignore these functions, there are neither tested nor documented, use them for fun if you like but dont ask me ;-)
175
+
176
+ def fill_with_sth(sth, size)
177
+ ary_length = self.size
178
+ diff = size - ary_length
179
+ if diff > 0
180
+ pop = Array.new(diff){|i| sth}
181
+ new_ary = pop.push(self).flatten!
182
+ return new_ary
183
+ else
184
+ return self
185
+ end
186
+ end
187
+
188
+
189
+ # Generiert einen JSON-String aus einem Array mit Werten
190
+ def json_labels
191
+ json = "["
192
+ self.each_with_index {|l,i| json << '{"id": "%s", "text": "%s"}' % [i, l] ; json << "," unless i+1 == self.size}
193
+ json << "]"
194
+ end
195
+
196
+ def to_text(sep = "<br />")
197
+ if Rails.env == "development"
198
+ raise "#REMOVED - use .join() - (17.12.2013, 15:18, Florian Eck)"
199
+ else
200
+ self.join(sep)
201
+ end
202
+ end
203
+
204
+
205
+ def interpolate(filtered_value = nil, options = {})
206
+ begin
207
+
208
+ int_start_value = nil
209
+ int_end_value = nil
210
+ int_from = 0
211
+ int_to = 0
212
+ int_steps = 0
213
+
214
+ array_with_interpolation = []
215
+
216
+ self.each_with_index do |x,i|
217
+ if i > 0
218
+ if x != filtered_value
219
+ # Wenn der letze ein fehler war ist die strecke zuende
220
+ if self[i-1] == filtered_value
221
+
222
+ if i-1 == 0
223
+ # Berechnung anstieg der linearfunktion zwischen Start-und Endpunkt
224
+ #
225
+ m = (self[i+1]-x)
226
+
227
+ array_with_interpolation[0] = x-m
228
+ array_with_interpolation << x
229
+
230
+ else
231
+ int_end_value = x
232
+ int_to = i
233
+
234
+ # Berechnung anstieg der linearfunktion zwischen Start-und Endpunkt
235
+ m = (int_end_value-int_start_value).fdiv((int_to-int_from))
236
+
237
+ # Fülle fehlende werte mit werten (y = mx + b)
238
+ int_steps.times {|mi| array_with_interpolation << (int_start_value+((mi+1)*m)) }
239
+ array_with_interpolation << int_end_value
240
+
241
+ # Reset all values
242
+ int_start_value = int_end_value
243
+ int_end_value = nil
244
+ int_from = 0
245
+ int_to = 0
246
+ int_steps = 0
247
+ puts array_with_interpolation.inspect
248
+ end
249
+ else
250
+ int_start_value = x
251
+ array_with_interpolation << x
252
+ end
253
+ # Wenn letzer wert fehlerhaft
254
+ elsif i == self.size-1 && x == filtered_value
255
+ # Berechnung anstieg der linearfunktion zwischen Start-und Endpunkt
256
+ # Berechnung über gesamtes array
257
+ m = (array_with_interpolation.last-array_with_interpolation.first).fdiv(array_with_interpolation.size-1)
258
+ array_with_interpolation << (array_with_interpolation.last+m)
259
+ else
260
+ int_steps += 1
261
+ int_from = i-int_steps
262
+ end
263
+
264
+ else
265
+ int_start_value = x
266
+ array_with_interpolation << x
267
+ end
268
+
269
+ end
270
+
271
+ return array_with_interpolation
272
+ rescue
273
+ puts "WARNING: Interpolation not possible!".red
274
+ return self
275
+ end
276
+
277
+ end
278
+
279
+ end
280
+ end
data/lib/boolean.rb ADDED
@@ -0,0 +1,32 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ # Functions for True/FalseClass
4
+ module Boolean
5
+ [TrueClass, FalseClass].each do |bclass|
6
+
7
+ bclass.class_eval do
8
+
9
+ # return string if true/false
10
+ def to_real(t = "ja", f = "nein")
11
+ case self
12
+ when TrueClass
13
+ return t
14
+ else
15
+ return f
16
+ end
17
+ end
18
+
19
+ # return integer if true/false
20
+ def to_i
21
+ case self
22
+ when TrueClass
23
+ return 1
24
+ else
25
+ return 0
26
+ end
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+ end