attribute_column 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ attribute_column
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in attribute_column.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Sam Schenkman-Moore
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # AttributeColumn
2
+
3
+ When Rails' form_for (or, similarly, simple_form_for) looks at a model class, it calls column_for_attribute(attr_name) for each attribute that is represented in the form.
4
+
5
+ I made this single-file module to help respond to those requests.
6
+
7
+ ## Installation
8
+
9
+ gem install attribute_column
10
+
11
+ ## Usage
12
+
13
+ (I'm writing this extemporaneously, contact me if I've made mistakes or it isn't clear how this works)
14
+
15
+ class Model
16
+ include AttributeColumn
17
+
18
+ def self.add_attribute(attr_name, type = :string)
19
+ define_method attr_name do
20
+ instance_variable_get(attr_name)
21
+ end
22
+ define_method "#{attr_name}=" do |new_val|
23
+ instance_variable_set(attr_name, new_val)
24
+ end
25
+
26
+ attribute_column(attr_name, type)
27
+ end
28
+
29
+ add_attribute(:created_at, :datetime)
30
+ end
31
+
32
+ # Then, in a view
33
+ = simple_form_for(Model.new) do |f|
34
+ = f.created_at
35
+
36
+ The Rails form helpers call column_for_attribute, which wants names of attributes and their types. This helps provide that.
37
+
38
+ ## Contributing
39
+
40
+ 1. Fork it
41
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
42
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
43
+ 4. Push to the branch (`git push origin my-new-feature`)
44
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+
5
+ namespace 'test' do
6
+ test_files = FileList['spec/**/*_spec.rb']
7
+ integration_test_files = FileList['spec/**/*_integration_spec.rb']
8
+ unit_test_files = test_files - integration_test_files
9
+
10
+ desc "Run unit tests"
11
+ Rake::TestTask.new('unit') do |t|
12
+ t.libs.push "lib"
13
+ t.test_files = unit_test_files
14
+ t.verbose = false
15
+ end
16
+
17
+ desc "Run integration tests"
18
+ Rake::TestTask.new('integration') do |t|
19
+ t.libs.push "lib"
20
+ t.test_files = integration_test_files
21
+ t.verbose = false
22
+ end
23
+ end
24
+
25
+ #Rake::Task['test'].clear
26
+ desc "Run all tests"
27
+ task 'test' => %w[test:integration test:unit]
28
+ task 'default' => 'test'
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'attribute_column/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "attribute_column"
8
+ spec.version = AttributeColumn::VERSION
9
+ spec.authors = ["Sam Schenkman-Moore"]
10
+ spec.email = ["samsm@samsm.com"]
11
+ spec.description = %q{Apply types to attributes for use in form_for, etc.}
12
+ spec.summary = %q{Give attributes ActiveRecord-like types.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "actionpack"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,3 @@
1
+ module AttributeColumn
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,38 @@
1
+ require "attribute_column/version"
2
+ require 'ostruct'
3
+ require 'active_support/concern'
4
+
5
+ module AttributeColumn
6
+ extend ActiveSupport::Concern
7
+ class Attribute < OpenStruct
8
+ def number?
9
+ number
10
+ end
11
+ end
12
+
13
+ def self.generate(options)
14
+ type = options[:type]
15
+ attribute = Attribute.new(options)
16
+ case type
17
+ when :string
18
+ attribute.limit = 255
19
+ when :integer, :float
20
+ attribute.number = true
21
+ end
22
+ attribute
23
+ end
24
+
25
+ module ClassMethods
26
+ def columns_hash
27
+ @columns_hash ||= {}
28
+ end
29
+
30
+ def attribute_column(name, attribute_type)
31
+ columns_hash[name] = AttributeColumn.generate({name: name, type: attribute_type})
32
+ end
33
+ end
34
+
35
+ def column_for_attribute(attribute)
36
+ self.class.columns_hash[attribute]
37
+ end
38
+ end
@@ -0,0 +1,38 @@
1
+ require_relative 'minitest_helper'
2
+
3
+ describe AttributeColumn do
4
+ [:binary, :boolean, :date, :datetime, :decimal, :float, :integer,
5
+ :primary_key, :string, :text, :time, :timestamp].each do |type|
6
+ it "should respond to all ActiveRecord type #{type}" do
7
+ AttributeColumn.generate({type: type}).must_be_kind_of AttributeColumn::Attribute
8
+ end
9
+ end
10
+
11
+ it "should have 255 limit when string" do
12
+ AttributeColumn.generate({type: :string}).limit.must_equal 255
13
+ end
14
+
15
+ [:float, :integer].each do |type|
16
+ it "should be number? true when #{type}" do
17
+ AttributeColumn.generate({type: type}).number?.must_equal true
18
+ end
19
+ end
20
+
21
+ describe "applied to class" do
22
+ let(:columned_class) do
23
+ Class.new do
24
+ include AttributeColumn
25
+ end
26
+ end
27
+ let(:columned) { columned_class.new }
28
+
29
+ it "should have a columns_hash" do
30
+ columned_class.columns_hash.must_be_kind_of Hash
31
+ end
32
+
33
+ it "should store (and retrieve) a column type" do
34
+ columned_class.attribute_column(:title, :string)
35
+ columned.column_for_attribute(:title).type.must_equal :string
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gem 'minitest' # ensures we're using the gem, and not the built in MT
3
+
4
+ $:.unshift File.dirname(__FILE__) + "/../lib"
5
+
6
+ require 'minitest/autorun'
7
+ require 'minitest/spec'
8
+ require 'minitest/mock'
9
+
10
+ require 'attribute_column'
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attribute_column
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sam Schenkman-Moore
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-28 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: Apply types to attributes for use in form_for, etc.
63
+ email:
64
+ - samsm@samsm.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .ruby-gemset
71
+ - .ruby-version
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - attribute_column.gemspec
77
+ - lib/attribute_column.rb
78
+ - lib/attribute_column/version.rb
79
+ - spec/attribute_column_spec.rb
80
+ - spec/minitest_helper.rb
81
+ homepage: ''
82
+ licenses:
83
+ - MIT
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ segments:
95
+ - 0
96
+ hash: -4231271001903247609
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ none: false
99
+ requirements:
100
+ - - ! '>='
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ segments:
104
+ - 0
105
+ hash: -4231271001903247609
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 1.8.25
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Give attributes ActiveRecord-like types.
112
+ test_files:
113
+ - spec/attribute_column_spec.rb
114
+ - spec/minitest_helper.rb