acts_as_railable 0.2.0 → 0.5.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c78756ddbf5d7766d77bf22d256cef16e2f283d65e67a0e8b0a166ab530ba440
4
- data.tar.gz: ea8ebf6d556b81eb532098a29735cce81ce64f3f92b5315ca22761f5f2531692
3
+ metadata.gz: dca44f5e8ecb10f0df71b4cd69440a390d5e16a51eb46fb7cb00c7c60e300531
4
+ data.tar.gz: 2f62a71649ac7b56e6c18e6851f7c8e744646aa931de5626f0db28ac014d88fa
5
5
  SHA512:
6
- metadata.gz: 52f287a70f5beaaa1a2cbe20da7522637c3a9d44aa2f6533cee285b91f52f9e57ceb6a1633326881f1f872b27cc3a8f2e6da40cbe11379ec0d4379e993531623
7
- data.tar.gz: fb7ee2c57767a3f1a70a98a6c28d5fc9c9bb93ec9414be1fba17ae46613991b83b94c7caf897ca3b4418eed447575acb5bdfe2af533183d6ff37b06ce9ec1785
6
+ metadata.gz: d782bb1b1a63ba146f95d5b25df44a1b0b8bd6b6641d1e408e761a2581ead1c4d4eff6763ce8be3b3783432e3bc5a6a2dc93b6a967cd9cba9710c94ca57f439d
7
+ data.tar.gz: c5fe17f407b9d7abc7882707e569ed0eb655cdb911e996bbda8cc850f1afe699ae01073e007432d175484049cce8845803e8ed2a61f9d3e68a1876a2e0830a41
@@ -7,34 +7,40 @@ module ActsAsRailable
7
7
  self.human_attribute_name attr
8
8
  end
9
9
 
10
- def human_action_name action, views="defaults"
11
- I18n.t("#{views}.action.#{action}", model: self.model_name.human)
10
+ def i18n_action_name name, default_name=nil
11
+ action_key = "#{self.table_name}.action.#{name}"
12
+ default_key = "application.action.#{name}" # unless I18n.exists? key
13
+ default_name ||= name.to_s.capitalize
14
+ I18n.t(action_key, model: self.model_name.human, default: [ default_key.to_sym, default_name ])
12
15
  end
13
16
 
14
- def human_enum_name(enum_name, enum_value)
17
+ # i18n_enum_name(:status, :edited)
18
+ def i18n_enum_name(enum_name, enum_value)
15
19
  self.human_attribute_name "#{enum_name}.#{enum_value}"
16
20
  end
17
21
 
18
- def human_enum_choices enum_name
22
+ # enum status: { edited: 0, published: 1, approved: 2, trashed: 3 }
23
+ # i18n_enum_choices(:status)
24
+ def i18n_enum_choices enum_name
19
25
  enum_hash = self.public_send enum_name.to_s.pluralize
20
- enum_hash.keys.map { |enum_value| [ self.human_enum_name(enum_name, enum_value), enum_value ] }
26
+ enum_hash.keys.map { |enum_value| [ self.i18n_enum_name(enum_name, enum_value), enum_value ] }
21
27
  end
22
28
 
23
- # constants
24
29
  # CURRENCIES = %w( CNY USD EUR GBP JPY )
25
- def human_array_choices enum_name
30
+ # i18n_array_choices(:currency)
31
+ def i18n_array_choices enum_name
26
32
  array_values = self.const_get enum_name.to_s.pluralize.upcase
27
- array_values.map { |enum_value| [ self.human_enum_name(enum_name, enum_value), enum_value ] }
33
+ array_values.map { |enum_value| [ self.i18n_enum_name(enum_name, enum_value), enum_value ] }
28
34
  end
29
35
  end
30
36
 
31
37
  def i18n_attribute_name attr
32
- self.class.i18n_attribute_name attr
38
+ self.class.human_attribute_name attr
33
39
  end
34
40
 
35
- def human_enum_name enum_name
41
+ def i18n_enum_name enum_name
36
42
  enum_value = self.public_send enum_name
37
- self.class.human_enum_name enum_name, enum_value
43
+ self.class.i18n_enum_name enum_name, enum_value
38
44
  end
39
45
  end
40
46
  end
@@ -3,7 +3,7 @@ module ActsAsRailable
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
- # database: local date
6
+ # database: date
7
7
  # parameter: local date
8
8
  scope :date_field_interval, -> (field, d1, d2) {
9
9
  tname = self.table_name
@@ -1,4 +1,9 @@
1
+ require 'acts_as_railable/view_helpers'
2
+
1
3
  module ActsAsRailable
2
4
  class Railtie < ::Rails::Railtie
5
+ initializer "acts_as_railable.view_helpers" do
6
+ ActiveSupport.on_load(:action_view) { include ActsAsRailable::ViewHelpers }
7
+ end
3
8
  end
4
9
  end
@@ -0,0 +1,95 @@
1
+ module Recordable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ end
6
+
7
+ # class methods
8
+ module ClassMethods
9
+ def record_name
10
+ self.model_name.singular
11
+ end
12
+
13
+ def form_error_placeholder_id
14
+ "#{self.record_name}_error_placeholder_id"
15
+ end
16
+
17
+ def titlebar_placeholder_id
18
+ "#{self.record_name}_titlebar_placeholder_id"
19
+ end
20
+
21
+ def navbar_placeholder_id
22
+ "#{self.record_name}_navbar_placeholder_id"
23
+ end
24
+
25
+ def collection_placeholder_id record_parent=nil
26
+ if record_parent.nil?
27
+ "#{self.record_name}_collection_placeholder_id"
28
+ else
29
+ "#{record_parent.record_name}_#{record_parent.id}_#{self.record_name}_collection_placeholder_id"
30
+ end
31
+ end
32
+
33
+ def pagination_placeholder_id
34
+ "#{self.record_name}_pagination_placeholder_id"
35
+ end
36
+
37
+ def script_placeholder_id
38
+ "#{self.record_name}_script_placeholder_id"
39
+ end
40
+
41
+ def modal_id
42
+ "#{self.record_name}_modal_id"
43
+ end
44
+
45
+ def modal_placeholder_id
46
+ "#{self.record_name}_modal_placeholder_id"
47
+ end
48
+
49
+ # override methods
50
+ def per_page
51
+ nil
52
+ end
53
+ end
54
+
55
+ # instance methods
56
+ def record_parent
57
+ nil
58
+ end
59
+
60
+ def record_name
61
+ self.class.record_name
62
+ end
63
+
64
+ def form_error_placeholder_id
65
+ self.class.form_error_placeholder_id
66
+ end
67
+
68
+ def titlebar_placeholder_id
69
+ self.class.titlebar_placeholder_id
70
+ end
71
+
72
+ def navbar_placeholder_id
73
+ self.class.navbar_placeholder_id
74
+ end
75
+
76
+ def collection_placeholder_id record_parent=nil
77
+ self.class.collection_placeholder_id record_parent
78
+ end
79
+
80
+ def pagination_placeholder_id
81
+ self.class.pagination_placeholder_id
82
+ end
83
+
84
+ def script_placeholder_id
85
+ self.class.script_placeholder_id
86
+ end
87
+
88
+ def modal_id
89
+ self.class.modal_id
90
+ end
91
+
92
+ def modal_placeholder_id
93
+ self.class.modal_placeholder_id
94
+ end
95
+ end
@@ -0,0 +1,62 @@
1
+ module ActsAsRailable
2
+ module Routable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ def self.polymorphic_path(record_or_hash_or_array, options = {})
9
+ Rails.application.routes.url_helpers.polymorphic_path(record_or_hash_or_array, options)
10
+ end
11
+
12
+ def self.polymorphic_url(record_or_hash_or_array, options = {})
13
+ Rails.application.routes.url_helpers.polymorphic_url(record_or_hash_or_array, options)
14
+ end
15
+
16
+ def self.record_action_path record_or_hash_or_array, record_self, action, options={}
17
+ record_or_hash_or_array = Array(record_or_hash_or_array)
18
+ record_or_hash_or_array << record_self if record_or_hash_or_array.exclude?(record_self)
19
+ if action.present? && %w(index show).exclude?(action.to_s)
20
+ options.merge!({action: action})
21
+ end
22
+ self.polymorphic_path record_or_hash_or_array, options
23
+ end
24
+
25
+ def self.record_action_url record_or_hash_or_array, record_self, action, options={}
26
+ record_or_hash_or_array = Array(record_or_hash_or_array)
27
+ record_or_hash_or_array << record_self if record_or_hash_or_array.exclude?(record_self)
28
+ if action.present? && %w(index show).exclude?(action.to_s)
29
+ options.merge!({action: action})
30
+ end
31
+ self.polymorphic_url record_or_hash_or_array, options
32
+ end
33
+
34
+ # class methods
35
+ module ClassMethods
36
+ def polymorphic_path(record_or_hash_or_array=[], options = {})
37
+ ActsAsRailable::Routable.polymorphic_path(record_or_hash_or_array, options)
38
+ end
39
+
40
+ def polymorphic_url(record_or_hash_or_array, options = {})
41
+ ActsAsRailable::Routable.polymorphic_path(record_or_hash_or_array, options)
42
+ end
43
+
44
+ def action_path action=nil, record_or_hash_or_array=[], options={}
45
+ ActsAsRailable::Routable.record_action_path record_or_hash_or_array, self, action, options
46
+ end
47
+
48
+ def action_url action=nil, record_or_hash_or_array=[], options={}
49
+ ActsAsRailable::Routable.record_action_url record_or_hash_or_array, self, action, options
50
+ end
51
+ end
52
+
53
+ # instance methods
54
+ def action_path action=nil, record_or_hash_or_array=[], options={}
55
+ ActsAsRailable::Routable.record_action_path record_or_hash_or_array, self, action, options
56
+ end
57
+
58
+ def action_url action=nil, record_or_hash_or_array=[], options={}
59
+ ActsAsRailable::Routable.record_action_url record_or_hash_or_array, self, action, options
60
+ end
61
+ end
62
+ end
@@ -1,3 +1,3 @@
1
1
  module ActsAsRailable
2
- VERSION = "0.2.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -0,0 +1,142 @@
1
+ module ActsAsRailable
2
+ module ViewHelpers
3
+ def void_link_to name, html_options=nil
4
+ link_to name, "javascript:void(0);", html_options
5
+ end
6
+
7
+ def back_link_to name, html_options
8
+ link_to name, "javascript:history.go(-1);", html_options
9
+ end
10
+
11
+ def native_flash_messages!
12
+ html = ""
13
+ flash.each do |name, msg|
14
+ if msg.is_a? String
15
+ color_code = name == 'notice' ? 'green' : 'red'
16
+ flash_style = "color: #{color_code}"
17
+ html += <<-HTML
18
+ <p style="#{flash_style}">
19
+ #{msg}
20
+ </p>
21
+ HTML
22
+ end
23
+ end
24
+ html.html_safe
25
+ end
26
+
27
+ def bootstrap_flash_messages!
28
+ html = ""
29
+ flash.each do |name, msg|
30
+ if msg.is_a? String
31
+ color_code = name == 'notice' ? 'success' : 'danger'
32
+ flash_class = "alert alert-#{color_code} alert-dismissible fade show"
33
+ html += <<-HTML
34
+ <div class="#{flash_class}" role="alert">
35
+ <div>#{msg}</div>
36
+ <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
37
+ </div>
38
+ HTML
39
+ end
40
+ end
41
+ html.html_safe
42
+ end
43
+
44
+ def native_form_error_messages!(object)
45
+ form_error_id = object.try(:form_error_placeholder_id) || "error_explanation"
46
+
47
+ if object.errors.any?
48
+ messages = object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
49
+ sentence = I18n.t("errors.template.header",
50
+ count: object.errors.count,
51
+ model: object.class.model_name.human.downcase)
52
+ bodyhead = I18n.t("errors.template.body")
53
+
54
+ html = <<-HTML
55
+ <div id="#{form_error_id}" style="color: red">
56
+ <h2>#{sentence}</h2>
57
+ <ul>#{messages}</ul>
58
+ </div>
59
+ HTML
60
+ else
61
+ html = <<-HTML
62
+ <div id="#{form_error_id}">
63
+ </div>
64
+ HTML
65
+ end
66
+
67
+ html.html_safe
68
+ end
69
+
70
+ def bootstrap_form_error_messages!(object)
71
+ form_error_id = object.try(:form_error_placeholder_id) || "error_explanation"
72
+
73
+ if object.errors.any?
74
+ messages = object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
75
+ sentence = I18n.t("errors.template.header",
76
+ count: object.errors.count,
77
+ model: object.class.model_name.human.downcase)
78
+ bodyhead = I18n.t("errors.template.body")
79
+
80
+ html = <<-HTML
81
+ <div id="#{form_error_id}">
82
+ <div class="alert alert-danger" role="alert">
83
+ <h6 class="alert-heading">#{sentence}</h6>
84
+ <ul class="mb-0">#{messages}</ul>
85
+ </div>
86
+ </div>
87
+ HTML
88
+ else
89
+ html = <<-HTML
90
+ <div id="#{form_error_id}">
91
+ </div>
92
+ HTML
93
+ end
94
+
95
+ html.html_safe
96
+ end
97
+
98
+ def native_devise_error_messages!(resource)
99
+ return "" if resource.errors.empty?
100
+
101
+ form_error_id = object.try(:form_error_placeholder_id) || "error_explanation"
102
+
103
+ messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
104
+ sentence = I18n.t("errors.messages.not_saved",
105
+ count: resource.errors.count,
106
+ resource: resource.class.model_name.human.downcase)
107
+ bodyhead = I18n.t("errors.template.body")
108
+
109
+ html = <<-HTML
110
+ <div id="#{form_error_id}">
111
+ <h2>#{sentence}</h2>
112
+ <ul>#{messages}</ul>
113
+ </div>
114
+ HTML
115
+
116
+ html.html_safe
117
+ end
118
+
119
+ def bootstrap_devise_error_messages!(resource)
120
+ return "" if resource.errors.empty?
121
+
122
+ form_error_id = object.try(:form_error_placeholder_id) || "error_explanation"
123
+
124
+ messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
125
+ sentence = I18n.t("errors.messages.not_saved",
126
+ count: resource.errors.count,
127
+ resource: resource.class.model_name.human.downcase)
128
+ bodyhead = I18n.t("errors.template.body")
129
+
130
+ html = <<-HTML
131
+ <div id="#{form_error_id}">
132
+ <div class="alert alert-danger" role="alert">
133
+ <h6 class="alert-heading">#{sentence}</h6>
134
+ <ul class="mb-0">#{messages}</ul>
135
+ </div>
136
+ </div>
137
+ HTML
138
+
139
+ html.html_safe
140
+ end
141
+ end
142
+ end
@@ -1,8 +1,9 @@
1
1
  require "acts_as_railable/version"
2
- require "acts_as_railable/railtie"
3
2
  require "acts_as_railable/core_ext"
4
- require "acts_as_railable/interval"
5
3
  require "acts_as_railable/humanable"
4
+ require "acts_as_railable/interval"
5
+ require "acts_as_railable/routable"
6
+ require "acts_as_railable/railtie" if defined?(Rails)
6
7
 
7
8
  module ActsAsRailable
8
9
  # Your code goes here...
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: acts_as_railable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Li Qi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-04-01 00:00:00.000000000 Z
11
+ date: 2022-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -39,7 +39,10 @@ files:
39
39
  - lib/acts_as_railable/humanable.rb
40
40
  - lib/acts_as_railable/interval.rb
41
41
  - lib/acts_as_railable/railtie.rb
42
+ - lib/acts_as_railable/recordable.rb
43
+ - lib/acts_as_railable/routable.rb
42
44
  - lib/acts_as_railable/version.rb
45
+ - lib/acts_as_railable/view_helpers.rb
43
46
  - lib/tasks/acts_as_railable_tasks.rake
44
47
  homepage: http://github.com/cloudbsd/acts_as_moonable
45
48
  licenses: