acts_as_railable 0.4.0 → 0.6.0

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
  SHA256:
3
- metadata.gz: fdd0050a08bd25499046cc5913df3966af963cb4020f196772b4c67659a85a80
4
- data.tar.gz: 3aac2d44904b4b9bbe1abc1fd98695c5686b0b0f85b82714af6e957c454af18c
3
+ metadata.gz: 5b66f87748b72def1cd866dd440815bdc3adf4c0a8289a204d754fa876715676
4
+ data.tar.gz: 69e3aa3261ba65dbf414643b0838471c571ae5cce15fc49046e9eeb9e5b6594b
5
5
  SHA512:
6
- metadata.gz: 7c79fc4c2c54a8038d2a1cc900d10f021f1e09c92b573031ec30217d27242554fb42c82e447536d5a8d04113e858c392e01d4bc7af10014284fd3e0870b3d072
7
- data.tar.gz: 529a059eed3e9b18d85a64bbf81b7d6e996398e4aec9001f1b56c536bf355d833a85cccfeae749af72b49262d8d95a4f190e9741ade795ac5d04f7893c7bef33
6
+ metadata.gz: f12a17b7db1a93b59fad23d038a378044097682434f0a5715b2c87b7e154113b77446be3d3e4413b8b9e7308e276fb33f28ef3e94827a035f8e33b1b070fa68f
7
+ data.tar.gz: 5f66ccaf9bba349420be4a338316508d87c51f68aff987f9140ea035d1ffa9357d0f7d823d5dc2c28f15fdd87c62472bab68e34c4e2ae58bae8bbe51f08942b0
@@ -9,7 +9,7 @@ module ActsAsRailable
9
9
 
10
10
  def i18n_action_name name, default_name=nil
11
11
  action_key = "#{self.table_name}.action.#{name}"
12
- default_key = "application.action.#{name}" # unless I18n.exists? key
12
+ default_key = "helper.action.#{name}" # unless I18n.exists? key
13
13
  default_name ||= name.to_s.capitalize
14
14
  I18n.t(action_key, model: self.model_name.human, default: [ default_key.to_sym, default_name ])
15
15
  end
@@ -1,9 +1,29 @@
1
1
  require 'acts_as_railable/view_helpers'
2
+ require "rails"
2
3
 
3
4
  module ActsAsRailable
4
5
  class Railtie < ::Rails::Railtie
5
- initializer "acts_as_railable.view_helpers" do
6
+ initializer "acts_as_railable" do |app|
6
7
  ActiveSupport.on_load(:action_view) { include ActsAsRailable::ViewHelpers }
8
+
9
+ ActsAsRailable::Railtie.instance_eval do
10
+ pattern = pattern_from app.config.i18n.available_locales
11
+ add("rails/locales/#{pattern}.yml")
12
+ end
13
+ end
14
+
15
+ protected
16
+
17
+ def self.add(pattern)
18
+ files = Dir[File.join(File.dirname(__FILE__), "../..", pattern)]
19
+
20
+ I18n.load_path.concat(files)
21
+ end
22
+
23
+ def self.pattern_from(args)
24
+ array = Array(args || [])
25
+ array.blank? ? "*" : "{#{array.join ','}}"
7
26
  end
8
27
  end
9
28
  end
29
+
@@ -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.4.0"
2
+ VERSION = "0.6.0"
3
3
  end
@@ -5,7 +5,7 @@ module ActsAsRailable
5
5
  end
6
6
 
7
7
  def back_link_to name, html_options
8
- link_to name, :back, html_options
8
+ link_to name, "javascript:history.go(-1);", html_options
9
9
  end
10
10
 
11
11
  def native_flash_messages!
@@ -42,6 +42,8 @@ module ActsAsRailable
42
42
  end
43
43
 
44
44
  def native_form_error_messages!(object)
45
+ form_error_id = object.try(:form_error_placeholder_id) || "error_explanation"
46
+
45
47
  if object.errors.any?
46
48
  messages = object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
47
49
  sentence = I18n.t("errors.template.header",
@@ -50,14 +52,14 @@ module ActsAsRailable
50
52
  bodyhead = I18n.t("errors.template.body")
51
53
 
52
54
  html = <<-HTML
53
- <div id="#{object.form_error_placeholder_id}" style="color: red">
55
+ <div id="#{form_error_id}" style="color: red">
54
56
  <h2>#{sentence}</h2>
55
57
  <ul>#{messages}</ul>
56
58
  </div>
57
59
  HTML
58
60
  else
59
61
  html = <<-HTML
60
- <div id="#{object.form_error_placeholder_id}">
62
+ <div id="#{form_error_id}">
61
63
  </div>
62
64
  HTML
63
65
  end
@@ -66,6 +68,8 @@ module ActsAsRailable
66
68
  end
67
69
 
68
70
  def bootstrap_form_error_messages!(object)
71
+ form_error_id = object.try(:form_error_placeholder_id) || "error_explanation"
72
+
69
73
  if object.errors.any?
70
74
  messages = object.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
71
75
  sentence = I18n.t("errors.template.header",
@@ -74,7 +78,7 @@ module ActsAsRailable
74
78
  bodyhead = I18n.t("errors.template.body")
75
79
 
76
80
  html = <<-HTML
77
- <div id="#{object.form_error_placeholder_id}">
81
+ <div id="#{form_error_id}">
78
82
  <div class="alert alert-danger" role="alert">
79
83
  <h6 class="alert-heading">#{sentence}</h6>
80
84
  <ul class="mb-0">#{messages}</ul>
@@ -83,7 +87,7 @@ module ActsAsRailable
83
87
  HTML
84
88
  else
85
89
  html = <<-HTML
86
- <div id="#{object.form_error_placeholder_id}">
90
+ <div id="#{form_error_id}">
87
91
  </div>
88
92
  HTML
89
93
  end
@@ -94,6 +98,8 @@ module ActsAsRailable
94
98
  def native_devise_error_messages!(resource)
95
99
  return "" if resource.errors.empty?
96
100
 
101
+ form_error_id = resource.try(:form_error_placeholder_id) || "error_explanation"
102
+
97
103
  messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
98
104
  sentence = I18n.t("errors.messages.not_saved",
99
105
  count: resource.errors.count,
@@ -101,7 +107,7 @@ module ActsAsRailable
101
107
  bodyhead = I18n.t("errors.template.body")
102
108
 
103
109
  html = <<-HTML
104
- <div id="error_explanation">
110
+ <div id="#{form_error_id}">
105
111
  <h2>#{sentence}</h2>
106
112
  <ul>#{messages}</ul>
107
113
  </div>
@@ -113,6 +119,8 @@ module ActsAsRailable
113
119
  def bootstrap_devise_error_messages!(resource)
114
120
  return "" if resource.errors.empty?
115
121
 
122
+ form_error_id = resource.try(:form_error_placeholder_id) || "error_explanation"
123
+
116
124
  messages = resource.errors.full_messages.map { |msg| content_tag(:li, msg) }.join
117
125
  sentence = I18n.t("errors.messages.not_saved",
118
126
  count: resource.errors.count,
@@ -120,10 +128,12 @@ module ActsAsRailable
120
128
  bodyhead = I18n.t("errors.template.body")
121
129
 
122
130
  html = <<-HTML
123
- <div class="alert alert-danger" role="alert">
124
- <h6>#{sentence}</h6>
125
- <ul class="my-0">#{messages}</ul>
126
- </div>
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>
127
137
  HTML
128
138
 
129
139
  html.html_safe
@@ -1,8 +1,9 @@
1
1
  require "acts_as_railable/version"
2
- require "acts_as_railable/railtie" if defined?(Rails)
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.4.0
4
+ version: 0.6.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-22 00:00:00.000000000 Z
11
+ date: 2022-05-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -39,6 +39,8 @@ 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
43
45
  - lib/acts_as_railable/view_helpers.rb
44
46
  - lib/tasks/acts_as_railable_tasks.rake