activerecord-traits 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5a036911309ec08f65047cc51cef3c7599baab8d
4
- data.tar.gz: da39b9604d3169ee2ab658c9187a8eb31d94ee84
3
+ metadata.gz: 84c79ad24f867dd858de441a1d0bef4d74c847d2
4
+ data.tar.gz: 9d312a4e8fff3e8330010e1745c687a6a072ce58
5
5
  SHA512:
6
- metadata.gz: 3197336a5215c5ef877158f34895b0cb151abf8c4c201e47cd1fe56217a830eea0720e25dd0d7bb2e80787a68081c1257b2704cef58ffb809ecdef38d06ec5f2
7
- data.tar.gz: a45aacb9e2721a52efef6d7dc77ee4092c93aac881a0f07645ba1ba03e7b425567212cad3d70b4774b7b5e529aaaac1ee693fd8dd13e5c2237e16f6acfb1ad51
6
+ metadata.gz: 2ec766c0c8f9da4fb1b66e35fdc7e0e02d7b11dc02d71255b3363e8af2843afb6cf0645a60a63fe0cb41ce47ad893fbfe4d1ca18981d946a8078678a7d65edcd
7
+ data.tar.gz: f3c97ed4ccf20b06b485b8d99b6485bca306b3bba31a6717e2e44e53096695bb3ae47597a3c8f0047ed29c247c132f8fe95ee6aa5f3a022dca217827cb3fb8cf
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+ # frozen_string_literal: true
3
+
4
+ module Traits
5
+ class Model
6
+ module I18n
7
+ def i18n_path
8
+ @i18n_path ||= "activerecord.models.#{name(:underscore)}"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -5,49 +5,58 @@ module Traits
5
5
  class Model
6
6
  module Naming
7
7
  # class User
8
- # name => user
8
+ # name => user
9
+ # name(:underscore) => user
9
10
  #
10
11
  # class Assets::Photo
11
- # name => assets/photo
12
+ # name => assets/photo
13
+ # name(:underscore) => assets_photo
12
14
  #
13
- def name
14
- # Rails 4.1 doesn't support nested acronyms
15
+ def name(namecase = nil)
16
+ # Rails 4.1 doesn't support nested acronyms.
15
17
  #
16
- # Rails 4.1 behaviour:
17
- # 'Helpers::ViewHelper'.underscore => 'helpers/view_helper' - good
18
- # 'CIHelper'.underscore => 'ci_helper' - good
19
- # 'Helpers::CIHelper' => 'helpers/ci_helper' - good
18
+ # Suppose you have:
19
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
20
+ # inflect.acronym 'CI'
21
+ # end
20
22
  #
21
- # 'Helpers::ViewHelper' => 'helpers/view_helper' - good
22
- # 'CIHelper' => 'cihelper' - good
23
- # 'Helpers::CIHelper' => 'helpers/ci_helper' - bad!
23
+ # Rails 4.1 underscore behaviour:
24
+ # 'Helpers::ViewHelper' => 'helpers/view_helper' - good
25
+ # 'CIHelper' => 'ci_helper' - good
26
+ # 'Helpers::CIHelper' => 'helpers/ci_helper' - good
24
27
  #
25
- # Newer Rails behaviour:
26
- # 'Helpers::ViewHelper' => 'helpers/view_helper' - good
27
- # 'CIHelper' => 'cihelper' - good
28
- # 'Helpers::CIHelper' => 'helpers/cihelper' - good
28
+ # 'Helpers::ViewHelper' => 'helpers/view_helper' - good
29
+ # 'CIHelper' => 'cihelper' - good
30
+ # 'Helpers::CIHelper' => 'helpers/ci_helper' - bad
31
+ #
32
+ # Newer Rails underscore behaviour:
33
+ # 'Helpers::ViewHelper' => 'helpers/view_helper' - good
34
+ # 'CIHelper' => 'cihelper' - good
35
+ # 'Helpers::CIHelper' => 'helpers/cihelper' - good
29
36
  #
30
37
  @name ||= model_class.name.split('::').map(&:underscore).join('/')
38
+ if namecase == :underscore
39
+ @underscore_name ||= @name.tr('/', '_')
40
+ else
41
+ @name
42
+ end
31
43
  end
32
44
 
33
45
  # class User
34
- # plural_name => users
46
+ # plural_name => users
47
+ # plural_name(:underscore) => users
35
48
  #
36
49
  # class Assets::Photo
37
- # plural_name => assets/photos
50
+ # plural_name => assets/photos
51
+ # plural_name(:underscore) => assets_photos
38
52
  #
39
- def plural_name
53
+ def plural_name(namecase = nil)
40
54
  @plural_name ||= name.pluralize
41
- end
42
-
43
- # class User
44
- # resource_name => users
45
- #
46
- # class Assets::Photo
47
- # resource_name => assets/photos
48
- #
49
- def resource_name
50
- @resource_name ||= plural_name
55
+ if namecase == :underscore
56
+ @plural_underscore_name ||= @plural_name.tr('/', '_')
57
+ else
58
+ @plural_name
59
+ end
51
60
  end
52
61
 
53
62
  # class User
@@ -61,17 +70,30 @@ module Traits
61
70
  end
62
71
 
63
72
  # class User
64
- # lookup_name => User
73
+ # lookup_name => User
65
74
  # lookup_name(:underscore) => user
66
75
  #
67
76
  # class Assets::Photo
68
- # lookup_name => AssetsPhoto
77
+ # lookup_name => AssetsPhoto
69
78
  # lookup_name(:underscore) => assets_photo
70
79
  #
71
- def lookup_name(namecase = :class)
72
- namecase == :underscore ?
73
- @lookup_name_underscore_case ||= name.gsub('/', '_') :
74
- @lookup_name_class_case ||= class_name.gsub('::', '')
80
+ def lookup_name(namecase = nil)
81
+ if namecase == :underscore
82
+ name(namecase)
83
+ else
84
+ @lookup_name ||= class_name.tr('::', '')
85
+ end
86
+ end
87
+
88
+ # TODO Deprecate?
89
+ # class User
90
+ # resource_name => users
91
+ #
92
+ # class Assets::Photo
93
+ # resource_name => assets/photos
94
+ #
95
+ def resource_name
96
+ @resource_name ||= plural_name
75
97
  end
76
98
 
77
99
  def to_hash
data/lib/traits/model.rb CHANGED
@@ -11,6 +11,7 @@ require 'traits/model/polymorphism'
11
11
  require 'traits/model/inheritance'
12
12
  require 'traits/model/querying'
13
13
  require 'traits/model/essay_shortcuts'
14
+ require 'traits/model/i18n'
14
15
 
15
16
  module Traits
16
17
  class Model
@@ -19,6 +20,7 @@ module Traits
19
20
  include Polymorphism
20
21
  include Querying
21
22
  include EssayShortcuts
23
+ include I18n
22
24
 
23
25
  attr_reader :model_class
24
26
 
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Traits
5
- VERSION = '1.0.2'
5
+ VERSION = '1.0.3'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-traits
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yaroslav Konoplov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-21 00:00:00.000000000 Z
11
+ date: 2016-10-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -87,6 +87,7 @@ files:
87
87
  - lib/traits/list.rb
88
88
  - lib/traits/model.rb
89
89
  - lib/traits/model/essay_shortcuts.rb
90
+ - lib/traits/model/i18n.rb
90
91
  - lib/traits/model/inheritance.rb
91
92
  - lib/traits/model/naming.rb
92
93
  - lib/traits/model/polymorphism.rb