acts_as_railable 0.1.0 → 0.4.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: d5d54d944f56b70ed779d7be62d18e528106a3b71e03ac55c991c930aa15bab7
4
- data.tar.gz: f86a1c086b761eaf296ef55b17523d677da0aae07032c4cdb1e5a279ec5891e7
3
+ metadata.gz: fdd0050a08bd25499046cc5913df3966af963cb4020f196772b4c67659a85a80
4
+ data.tar.gz: 3aac2d44904b4b9bbe1abc1fd98695c5686b0b0f85b82714af6e957c454af18c
5
5
  SHA512:
6
- metadata.gz: c71ec735fdb453633cb9b5f56648d4e23a34f41349a0f74f1cc8ef087830cdb443303c0de04e1d22274cf46c8119241f25d60d356015a5bd98128b856d14db75
7
- data.tar.gz: a015bb53280646c958dc43a6636789175710c9815f0a55da793b3c05d3af8bd38240956a1bb01f2a26c1edb1e9b35bef858869d19240f3b23984519fc2f5d2aa
6
+ metadata.gz: 7c79fc4c2c54a8038d2a1cc900d10f021f1e09c92b573031ec30217d27242554fb42c82e447536d5a8d04113e858c392e01d4bc7af10014284fd3e0870b3d072
7
+ data.tar.gz: 529a059eed3e9b18d85a64bbf81b7d6e996398e4aec9001f1b56c536bf355d833a85cccfeae749af72b49262d8d95a4f190e9741ade795ac5d04f7893c7bef33
@@ -1,4 +1,4 @@
1
- module ActsAsMoonable
1
+ module ActsAsRailable
2
2
  module Humanable
3
3
  extend ActiveSupport::Concern
4
4
 
@@ -7,34 +7,40 @@ module ActsAsMoonable
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
@@ -1,9 +1,9 @@
1
- module ActsAsMoonable
1
+ module ActsAsRailable
2
2
  module Interval
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
@@ -1,3 +1,3 @@
1
1
  module ActsAsRailable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.4.0"
3
3
  end
@@ -0,0 +1,132 @@
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, :back, 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
+ if object.errors.any?
46
+ messages = object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
47
+ sentence = I18n.t("errors.template.header",
48
+ count: object.errors.count,
49
+ model: object.class.model_name.human.downcase)
50
+ bodyhead = I18n.t("errors.template.body")
51
+
52
+ html = <<-HTML
53
+ <div id="#{object.form_error_placeholder_id}" style="color: red">
54
+ <h2>#{sentence}</h2>
55
+ <ul>#{messages}</ul>
56
+ </div>
57
+ HTML
58
+ else
59
+ html = <<-HTML
60
+ <div id="#{object.form_error_placeholder_id}">
61
+ </div>
62
+ HTML
63
+ end
64
+
65
+ html.html_safe
66
+ end
67
+
68
+ def bootstrap_form_error_messages!(object)
69
+ if object.errors.any?
70
+ messages = object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
71
+ sentence = I18n.t("errors.template.header",
72
+ count: object.errors.count,
73
+ model: object.class.model_name.human.downcase)
74
+ bodyhead = I18n.t("errors.template.body")
75
+
76
+ html = <<-HTML
77
+ <div id="#{object.form_error_placeholder_id}">
78
+ <div class="alert alert-danger" role="alert">
79
+ <h6 class="alert-heading">#{sentence}</h6>
80
+ <ul class="mb-0">#{messages}</ul>
81
+ </div>
82
+ </div>
83
+ HTML
84
+ else
85
+ html = <<-HTML
86
+ <div id="#{object.form_error_placeholder_id}">
87
+ </div>
88
+ HTML
89
+ end
90
+
91
+ html.html_safe
92
+ end
93
+
94
+ def native_devise_error_messages!(resource)
95
+ return "" if resource.errors.empty?
96
+
97
+ messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
98
+ sentence = I18n.t("errors.messages.not_saved",
99
+ count: resource.errors.count,
100
+ resource: resource.class.model_name.human.downcase)
101
+ bodyhead = I18n.t("errors.template.body")
102
+
103
+ html = <<-HTML
104
+ <div id="error_explanation">
105
+ <h2>#{sentence}</h2>
106
+ <ul>#{messages}</ul>
107
+ </div>
108
+ HTML
109
+
110
+ html.html_safe
111
+ end
112
+
113
+ def bootstrap_devise_error_messages!(resource)
114
+ return "" if resource.errors.empty?
115
+
116
+ messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
117
+ sentence = I18n.t("errors.messages.not_saved",
118
+ count: resource.errors.count,
119
+ resource: resource.class.model_name.human.downcase)
120
+ bodyhead = I18n.t("errors.template.body")
121
+
122
+ html = <<-HTML
123
+ <div class="alert alert-danger" role="alert">
124
+ <h6>#{sentence}</h6>
125
+ <ul class="my-0">#{messages}</ul>
126
+ </div>
127
+ HTML
128
+
129
+ html.html_safe
130
+ end
131
+ end
132
+ end
@@ -1,6 +1,8 @@
1
1
  require "acts_as_railable/version"
2
- require "acts_as_railable/railtie"
2
+ require "acts_as_railable/railtie" if defined?(Rails)
3
3
  require "acts_as_railable/core_ext"
4
+ require "acts_as_railable/interval"
5
+ require "acts_as_railable/humanable"
4
6
 
5
7
  module ActsAsRailable
6
8
  # 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.1.0
4
+ version: 0.4.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-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,7 +24,7 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '5.2'
27
- description: ActsAsMoonable gem provides extended methods for rails.
27
+ description: ActsAsRailable gem provides extended methods for rails.
28
28
  email:
29
29
  - cloudbsd@qq.com
30
30
  executables: []
@@ -40,6 +40,7 @@ files:
40
40
  - lib/acts_as_railable/interval.rb
41
41
  - lib/acts_as_railable/railtie.rb
42
42
  - lib/acts_as_railable/version.rb
43
+ - lib/acts_as_railable/view_helpers.rb
43
44
  - lib/tasks/acts_as_railable_tasks.rake
44
45
  homepage: http://github.com/cloudbsd/acts_as_moonable
45
46
  licenses:
@@ -67,5 +68,5 @@ requirements: []
67
68
  rubygems_version: 3.3.7
68
69
  signing_key:
69
70
  specification_version: 4
70
- summary: Acts As Moonable Gem.
71
+ summary: Acts As Railable Gem.
71
72
  test_files: []