acts_as_concatenation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,13 @@
1
+ ActsAsConcatenation
2
+ ===================
3
+
4
+ Introduction goes here.
5
+
6
+
7
+ Example
8
+ =======
9
+
10
+ Example goes here.
11
+
12
+
13
+ Copyright (c) 2009 [name of plugin creator], released under the MIT license
@@ -0,0 +1,38 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the acts_as_concatenation plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the acts_as_concatenation plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'ActsAsConcatenation'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+
26
+ begin
27
+ require 'jeweler'
28
+ Jeweler::Tasks.new do |gemspec|
29
+ gemspec.name = "acts_as_concatenation"
30
+ gemspec.summary = "Making multiple values save in a single column as a concatenation of string"
31
+ gemspec.description = "Making multiple values save in a single column as a concatenation of string,including a ActiveRecord method and view helper"
32
+ gemspec.email = "haoinit@gmail.com"
33
+ gemspec.homepage = "http://github.com/hchen/acts_as_concatenation"
34
+ gemspec.authors = ["Hao Chen"]
35
+ end
36
+ rescue LoadError
37
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
38
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_concatenation}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Hao Chen"]
12
+ s.date = %q{2009-10-19}
13
+ s.description = %q{Making multiple values save in a single column as a concatenation of string,including a ActiveRecord method and view helper}
14
+ s.email = %q{haoinit@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "acts_as_concatenation.gemspec",
24
+ "init.rb",
25
+ "install.rb",
26
+ "lib/acts_as_concatenation.rb",
27
+ "lib/acts_as_concatenation/view_helper.rb",
28
+ "rails/init.rb",
29
+ "tasks/acts_as_concatenation_tasks.rake",
30
+ "test/acts_as_concatenation_test.rb",
31
+ "test/test_helper.rb",
32
+ "uninstall.rb"
33
+ ]
34
+ s.homepage = %q{http://github.com/hchen/acts_as_concatenation}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{Making multiple values save in a single column as a concatenation of string}
39
+ s.test_files = [
40
+ "test/acts_as_concatenation_test.rb",
41
+ "test/test_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ else
50
+ end
51
+ else
52
+ end
53
+ end
54
+
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ # Require the plugin code
2
+ require File.dirname(__FILE__) + "/rails/init"
3
+
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,30 @@
1
+ module ActsAsConcatenation
2
+ module ClassMethods
3
+ def acts_as_concatenation(*args)
4
+ options = args.extract_options!
5
+ separator = options.delete(:separator) || '|'
6
+ args.each do |attr|
7
+ class_eval do
8
+ define_method "#{attr}=".to_sym do |a|
9
+ if (!a.nil?) && (!a.empty?) && (a.respond_to? :join)
10
+ a.delete_if {|x| x.nil? || x.strip.empty? } #for a fake checkbox in html page to crack empty selection
11
+ self[attr] = a.join(separator)
12
+ else
13
+ self[attr] = a
14
+ end
15
+ end
16
+
17
+ define_method attr do
18
+ value = self[attr]
19
+ value.nil? ? [] : value.split(separator)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.included(base)
27
+ base.send :extend, ClassMethods
28
+ end
29
+ end
30
+
@@ -0,0 +1,70 @@
1
+ #
2
+ #
3
+ # View helper for displaying checkbox group for instead a traditional
4
+ # multiple-selected selectbox
5
+ #
6
+ # checkbox_group(:company, :company_types)
7
+ #
8
+ # the format of the multiple checked values in database is just like "a|b|c"
9
+ #
10
+ module ActsAsConcatenation
11
+ module ViewHelper
12
+
13
+ def translate_concatenation_value(keys, choices)
14
+ result = []
15
+ if keys && !keys.empty? && keys.is_a?(String)
16
+ keys.split('|').each do |k|
17
+ result << choices.rassoc(k.to_i).at(0) unless choices.rassoc(k.to_i).nil?
18
+ end
19
+ elsif keys && !keys.empty? && keys.is_a?(Array)
20
+ keys.each do |k|
21
+ result << choices.rassoc(k.to_i).at(0) unless choices.rassoc(k.to_i).nil?
22
+ end
23
+ end
24
+ return result.join(', ')
25
+ end
26
+
27
+ end
28
+ end
29
+
30
+
31
+
32
+
33
+ module ActionView
34
+ module Helpers
35
+
36
+ module FormHelper
37
+
38
+ def checkbox_group(object, method, choices, options = {})
39
+ InstanceTag.new(object, method, self, options.delete(:object)).to_checkbox_group_tag(choices, options)
40
+ end
41
+
42
+ end
43
+
44
+ class InstanceTag
45
+ def to_checkbox_group_tag(choices, options)
46
+ ulid = "#{tag_id}_ul"
47
+ real_values = value(object)||[]
48
+
49
+ tagname = tag_name + '[]'
50
+
51
+ s = "<input type='hidden' name='#{tagname}' value='' style='display:none;'/>"
52
+ s << "<ul id=#{ulid} class=selectbox-group>"
53
+ choices.each do |o|
54
+ if_checked = real_values.include?(o[1].to_s) ? "checked='checked'" : ""
55
+ s << "<li>"
56
+ s << "<input type='checkbox' name='#{tagname}' value='#{o[1]}' #{if_checked}/><label>#{o[0]}</label>"
57
+ s << "</li>"
58
+ end
59
+ s << "</ul>"
60
+ end
61
+ end
62
+
63
+ class FormBuilder
64
+ def checkbox_group(method, choices, options = {})
65
+ @template.checkbox_group(@object_name, method, choices, options.merge(:object => @object))
66
+ end
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,8 @@
1
+ require 'acts_as_concatenation'
2
+
3
+ ActiveRecord::Base.send :include, ActsAsConcatenation
4
+
5
+ if defined?(ActionView)
6
+ require 'acts_as_concatenation/view_helper'
7
+ ActionView::Base.send :include, ActsAsConcatenation::ViewHelper
8
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :acts_as_concatenation do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class ActsAsConcatenationTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_concatenation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hao Chen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-19 00:00:00 +08:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Making multiple values save in a single column as a concatenation of string,including a ActiveRecord method and view helper
17
+ email: haoinit@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - MIT-LICENSE
26
+ - README
27
+ - Rakefile
28
+ - VERSION
29
+ - acts_as_concatenation.gemspec
30
+ - init.rb
31
+ - install.rb
32
+ - lib/acts_as_concatenation.rb
33
+ - lib/acts_as_concatenation/view_helper.rb
34
+ - rails/init.rb
35
+ - tasks/acts_as_concatenation_tasks.rake
36
+ - test/acts_as_concatenation_test.rb
37
+ - test/test_helper.rb
38
+ - uninstall.rb
39
+ has_rdoc: true
40
+ homepage: http://github.com/hchen/acts_as_concatenation
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.3.5
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Making multiple values save in a single column as a concatenation of string
67
+ test_files:
68
+ - test/acts_as_concatenation_test.rb
69
+ - test/test_helper.rb