olson 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,4 +2,12 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in olson.gemspec
4
4
  gemspec
5
+
6
+ gem 'rake'
7
+ gem 'pry'
8
+
5
9
  gem 'rspec'
10
+ gem 'guard', git: 'https://github.com/guard/guard.git'
11
+ gem 'guard-rspec'
12
+ gem 'guard-shell'
13
+ gem 'rb-fsevent'
@@ -0,0 +1,115 @@
1
+ GIT
2
+ remote: https://github.com/guard/guard.git
3
+ revision: 1e853b2f8c9b092e79addc0a652e4d1a34eda224
4
+ specs:
5
+ guard (1.7.0)
6
+ formatador (>= 0.2.4)
7
+ listen (>= 0.6.0)
8
+ lumberjack (>= 1.0.2)
9
+ pry (>= 0.9.10)
10
+ thor (>= 0.14.6)
11
+
12
+ PATH
13
+ remote: .
14
+ specs:
15
+ olson (0.0.3)
16
+ activesupport
17
+
18
+ GEM
19
+ remote: https://rubygems.org/
20
+ specs:
21
+ actionpack (3.2.13)
22
+ activemodel (= 3.2.13)
23
+ activesupport (= 3.2.13)
24
+ builder (~> 3.0.0)
25
+ erubis (~> 2.7.0)
26
+ journey (~> 1.0.4)
27
+ rack (~> 1.4.5)
28
+ rack-cache (~> 1.2)
29
+ rack-test (~> 0.6.1)
30
+ sprockets (~> 2.2.1)
31
+ activemodel (3.2.13)
32
+ activesupport (= 3.2.13)
33
+ builder (~> 3.0.0)
34
+ activesupport (3.2.13)
35
+ i18n (= 0.6.1)
36
+ multi_json (~> 1.0)
37
+ builder (3.0.4)
38
+ coderay (1.0.9)
39
+ colorize (0.5.8)
40
+ coveralls (0.6.3)
41
+ colorize
42
+ multi_json (~> 1.3)
43
+ rest-client
44
+ simplecov (>= 0.7)
45
+ thor
46
+ diff-lcs (1.2.1)
47
+ draper (1.1.0)
48
+ actionpack (>= 3.0)
49
+ activesupport (>= 3.0)
50
+ request_store (~> 1.0.3)
51
+ erubis (2.7.0)
52
+ formatador (0.2.4)
53
+ guard-rspec (2.5.1)
54
+ guard (>= 1.1)
55
+ rspec (~> 2.11)
56
+ guard-shell (0.5.1)
57
+ guard (>= 1.1.0)
58
+ hike (1.2.1)
59
+ i18n (0.6.1)
60
+ journey (1.0.4)
61
+ listen (0.7.3)
62
+ lumberjack (1.0.3)
63
+ method_source (0.8.1)
64
+ mime-types (1.21)
65
+ multi_json (1.7.2)
66
+ pry (0.9.12)
67
+ coderay (~> 1.0.5)
68
+ method_source (~> 0.8)
69
+ slop (~> 3.4)
70
+ rack (1.4.5)
71
+ rack-cache (1.2)
72
+ rack (>= 0.4)
73
+ rack-test (0.6.2)
74
+ rack (>= 1.0)
75
+ rake (10.0.4)
76
+ rb-fsevent (0.9.3)
77
+ request_store (1.0.5)
78
+ rest-client (1.6.7)
79
+ mime-types (>= 1.16)
80
+ rspec (2.13.0)
81
+ rspec-core (~> 2.13.0)
82
+ rspec-expectations (~> 2.13.0)
83
+ rspec-mocks (~> 2.13.0)
84
+ rspec-core (2.13.1)
85
+ rspec-expectations (2.13.0)
86
+ diff-lcs (>= 1.1.3, < 2.0)
87
+ rspec-mocks (2.13.0)
88
+ simplecov (0.7.1)
89
+ multi_json (~> 1.0)
90
+ simplecov-html (~> 0.7.1)
91
+ simplecov-html (0.7.1)
92
+ slop (3.4.4)
93
+ sprockets (2.2.2)
94
+ hike (~> 1.2)
95
+ multi_json (~> 1.0)
96
+ rack (~> 1.0)
97
+ tilt (~> 1.1, != 1.3.0)
98
+ thor (0.18.0)
99
+ tilt (1.3.6)
100
+
101
+ PLATFORMS
102
+ ruby
103
+
104
+ DEPENDENCIES
105
+ activemodel
106
+ coveralls
107
+ draper
108
+ guard!
109
+ guard-rspec
110
+ guard-shell
111
+ olson!
112
+ pry
113
+ rake
114
+ rb-fsevent
115
+ rspec
@@ -0,0 +1,11 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', cli: '--format doc --color', keep_failed: false, all_after_pass: false do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/olson_spec.rb" }
8
+ watch('spec/spec_helper.rb') { "spec" }
9
+ watch(%r{^spec/fixtures/.*\.rb$}) { "spec" }
10
+ end
11
+
data/README.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  *Apparently it's a reference to Mad Men and Draper...*
4
4
 
5
+ [![Gem Version](https://badge.fury.io/rb/olson.png)](http://badge.fury.io/rb/olson)
6
+ [![Coverage Status](https://coveralls.io/repos/carnesmedia/olson/badge.png?branch=master)](https://coveralls.io/r/carnesmedia/olson)
5
7
  [![Code Climate](https://codeclimate.com/github/carnesmedia/olson.png)](https://codeclimate.com/github/carnesmedia/olson)
6
8
 
7
9
  Olson is a tool to help display identifiers to users. It’s basically a way to turn your decorator in to a wrapper for `.humanize` with automatic built-in support for I18n.
data/Rakefile CHANGED
@@ -1 +1,5 @@
1
1
  require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+ task :default => :spec
@@ -1,4 +1,4 @@
1
+ require 'active_support/concern'
1
2
  require 'olson/instance_methods'
2
3
  require 'olson/class_methods'
3
4
  require 'olson/version'
4
- require 'active_support/concern'
@@ -30,13 +30,16 @@ module ClassMethods
30
30
  attr_options = :"#{ attr }_options"
31
31
  define_singleton_method attr_options do # def status_options
32
32
  options_for_select_with_i18n attr, # options_for_select_with_i18n 'status',
33
- model_class.send(attr_options) # User.status_options
33
+ source_class.send(attr_options) # User.status_options
34
34
  end # end
35
35
  end
36
36
  end
37
37
 
38
38
  # Humanize an attribute using I18n, falling back to the humanized attributes value.
39
39
  #
40
+ # The `.default_humanize` method can be overridden to configure how values are humanized
41
+ # when they are not found in I18n.
42
+ #
40
43
  # I tend to store attributes like `status` or `role` as underscored strings (a string that
41
44
  # would be suitable for a method/variable name) sometimes a simple .humanize will do the
42
45
  # trick when it comes to displaying that value in the UI user but other times you need to
@@ -73,11 +76,28 @@ module ClassMethods
73
76
  # @user.decorator.plan_status # => 'Current'
74
77
  #
75
78
  # @user.activation_status = 'inactive'
76
- # @user.decorator.activation_status = 'Inactive'
77
- def humanize(attribute, key, default = key.to_s.humanize)
78
- i18n_with_scoped_defaults key, [model_name.i18n_key, attribute], default if key.present?
79
+ # @user.decorator.activation_status # => 'Inactive'
80
+ def humanize(attribute, value, default = default_humanize(value))
81
+ i18n_with_scoped_defaults value, [model_name.i18n_key, attribute], default if value.present?
79
82
  end
80
83
 
84
+ # The default way to humanize a value when not found in I18n.
85
+ #
86
+ # To configure the default humanization, you can override `.default_humanize` in your
87
+ # decorator. For example, to use `String#titleize` instead of `String#humanize`:
88
+ #
89
+ # # user_decorator.rb
90
+ # class UserDecorator < ApplicationDecorator
91
+ # decorates :user
92
+ #
93
+ # def self.default_humanize(value)
94
+ # value.to_s.titleize
95
+ # end
96
+ # end
97
+ #
98
+ def default_humanize(value)
99
+ value.to_s.humanize
100
+ end
81
101
 
82
102
  # Try to translate a key with I18n and a scope but fallback to less-and-less scope.
83
103
  # An example will explain more clearly:
@@ -2,7 +2,7 @@ module Olson
2
2
  extend ActiveSupport::Concern
3
3
 
4
4
  # See +ApplicationDecorator.humanize+
5
- def humanize(attribute, key = model.send(attribute), default = key.to_s.humanize)
6
- self.class.humanize attribute, key, default
5
+ def humanize(attribute, value = model.send(attribute), default = self.class.default_humanize(value))
6
+ self.class.humanize attribute, value, default
7
7
  end
8
8
  end
@@ -1,3 +1,3 @@
1
1
  module Olson
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,3 @@
1
+ class Draper::Decorator
2
+ include Olson
3
+ end
@@ -0,0 +1,19 @@
1
+ # Human and HumanDecorator is an example without ActiveModel or Draper
2
+
3
+
4
+ class Human
5
+ attr_accessor :status
6
+ def self.model_name
7
+ OpenStruct.new(i18n_key: "human")
8
+ end
9
+ end
10
+
11
+ class HumanDecorator < SimpleDelegator
12
+ include Olson
13
+ humanizes :status
14
+
15
+ alias_method :model, :__getobj__
16
+ def self.model_name
17
+ Human.model_name
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ # User and UserDecorator is an example with ActiveModel and Draper
2
+
3
+ class User
4
+ extend ActiveModel::Naming
5
+ attr_accessor :status
6
+
7
+ def self.status_options
8
+ %w[ approved rejected ]
9
+ end
10
+ end
11
+
12
+ class UserDecorator < Draper::Decorator
13
+ decorates :user
14
+ humanizes :status
15
+ end
16
+
17
+ class CustomUserDecorator < Draper::Decorator
18
+ decorates :user
19
+ humanizes :status
20
+
21
+ def self.default_humanize(value)
22
+ value.to_s.upcase
23
+ end
24
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe Olson do
4
+ context "when using Draper and ActiveModel" do
5
+ let(:object) { User.new }
6
+ let(:decorated) { UserDecorator.decorate(object) }
7
+
8
+ before { object.status = 'submitted' }
9
+
10
+ it "humanizes a field" do
11
+ decorated.status.should == "Submitted"
12
+ end
13
+
14
+ it 'humanizes with I18n when specified' do
15
+ object.status = 'rejected'
16
+ decorated.status.should == 'OMG Fail'
17
+ end
18
+
19
+ it 'humanizes options for select' do
20
+ UserDecorator.status_options.should == [
21
+ ["Approved", "approved"],
22
+ ["OMG Fail", "rejected"],
23
+ ]
24
+ end
25
+
26
+ context 'overriding the default behaviour' do
27
+ let(:decorated) { CustomUserDecorator.decorate(object) }
28
+
29
+ it "humanizes a field" do
30
+ decorated.status.should == "SUBMITTED"
31
+ end
32
+
33
+ it 'humanizes options for select' do
34
+ CustomUserDecorator.status_options.should == [
35
+ ["APPROVED", "approved"],
36
+ ["OMG Fail", "rejected"],
37
+ ]
38
+ end
39
+ end
40
+
41
+ end
42
+
43
+ context 'without draper' do
44
+ let(:object) { Human.new }
45
+ let(:decorated) { HumanDecorator.new(object) }
46
+
47
+ it 'humanizes a field' do
48
+ object.status = 'submitted'
49
+ decorated.status.should == 'Submitted'
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,32 @@
1
+
2
+ require 'simplecov'
3
+ require 'coveralls'
4
+
5
+ Coveralls.wear!
6
+
7
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
8
+ SimpleCov::Formatter::HTMLFormatter,
9
+ Coveralls::SimpleCov::Formatter,
10
+ ]
11
+
12
+ SimpleCov.start
13
+
14
+ require 'rubygems'
15
+ require 'bundler/setup'
16
+
17
+ require 'olson'
18
+ require 'ostruct'
19
+ require 'active_model'
20
+ require 'draper'
21
+ require 'draper_initializer'
22
+
23
+ spec_dir = Pathname.new(__FILE__).dirname
24
+
25
+ Dir[spec_dir.join("fixtures/**/*.rb")].each { |f| require f }
26
+ Dir[spec_dir.join("support/**/*.rb")].each { |f| require f }
27
+
28
+ I18n.config.load_path << spec_dir.join('fixtures/en.yml')
29
+
30
+ RSpec.configure do |config|
31
+ # some (optional) config here
32
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: olson
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2013-03-19 00:00:00.000000000 Z
13
+ date: 2013-08-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -28,6 +28,38 @@ dependencies:
28
28
  - - ! '>='
29
29
  - !ruby/object:Gem::Version
30
30
  version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: coveralls
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: activemodel
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
31
63
  - !ruby/object:Gem::Dependency
32
64
  name: rspec
33
65
  requirement: !ruby/object:Gem::Requirement
@@ -44,6 +76,22 @@ dependencies:
44
76
  - - ! '>='
45
77
  - !ruby/object:Gem::Version
46
78
  version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: draper
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
47
95
  description: Object-oriented humanize
48
96
  email:
49
97
  - kristian@kristianfreeman.com
@@ -57,9 +105,16 @@ files:
57
105
  - lib/olson/version.rb
58
106
  - lib/olson.rb
59
107
  - Gemfile
108
+ - Gemfile.lock
109
+ - Guardfile
60
110
  - LICENSE.txt
61
111
  - Rakefile
62
112
  - README.md
113
+ - spec/draper_initializer.rb
114
+ - spec/fixtures/human.rb
115
+ - spec/fixtures/user.rb
116
+ - spec/olson_spec.rb
117
+ - spec/spec_helper.rb
63
118
  homepage: http://github.com/carnesmedia/olson
64
119
  licenses: []
65
120
  post_install_message:
@@ -72,20 +127,30 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
127
  - - ! '>='
73
128
  - !ruby/object:Gem::Version
74
129
  version: '0'
130
+ segments:
131
+ - 0
132
+ hash: 2017626489828929657
75
133
  required_rubygems_version: !ruby/object:Gem::Requirement
76
134
  none: false
77
135
  requirements:
78
136
  - - ! '>='
79
137
  - !ruby/object:Gem::Version
80
138
  version: '0'
139
+ segments:
140
+ - 0
141
+ hash: 2017626489828929657
81
142
  requirements: []
82
143
  rubyforge_project:
83
- rubygems_version: 1.8.24
144
+ rubygems_version: 1.8.25
84
145
  signing_key:
85
146
  specification_version: 3
86
147
  summary: Olson is a tool to help display identifiers to users. It’s basically a way
87
148
  to turn your decorator in to a wrapper for `.humanize` with automatic built-in support
88
149
  for I18n. See [Using I18n and Draper to Render Database Attributes](http://blog.amielmartin.com/post/12341219947/using-i18n-and-draper-to-render-database-attributes)
89
150
  to read more about this concept.
90
- test_files: []
91
- has_rdoc:
151
+ test_files:
152
+ - spec/draper_initializer.rb
153
+ - spec/fixtures/human.rb
154
+ - spec/fixtures/user.rb
155
+ - spec/olson_spec.rb
156
+ - spec/spec_helper.rb