acts_as_railable 0.3.0 → 0.5.1

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: 566e969945d5fe9cb3c6e124626419490ad8a6bc2278def29d61b28d29826231
4
- data.tar.gz: 6eb4c7a8fcc9ba9249adb7d1323dc9f2d8820f4b646770f5f734c3bdd82b08e2
3
+ metadata.gz: b2f28e8fd9a682f88dca16efe015fcd5fa585991f6a7b5a34f31f53981877f91
4
+ data.tar.gz: 808737caabe8785296f14422d9e549dede063710e83f0f314599dae4df37ce3b
5
5
  SHA512:
6
- metadata.gz: 245088d2f7441d9be30703f38f728f801972af70c7941a5e2e28d7ba0da092a3aebae1efdebb4f576be3d639f78130be2d623767d983ef50d1117bc96c101620
7
- data.tar.gz: a050465502d8540ad9949e0221b95f37ad8cb8651597ae0bab6d9783a7be96ff6d4f394c953fb640fa87d1bdc7feb35ec62902fc9760c3b903877cb885ee6f97
6
+ metadata.gz: 9ddd43a1d7c0fa0f66170421525bcd26fbcba40ffa16103d0bf27704f7bc6f91065c877d5e39c8b0aa5df88c9e60482e370d13dbf0e580a9f5ae2e54bc419ffb
7
+ data.tar.gz: df376beca6362f1d17fcb3795efb06130a81c2b438b8a0110fe4c50484844925d55a010e727d6f095dc3194624712804c83846849f3c6a004a9bfcc7f5f51df9
@@ -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.3.0"
2
+ VERSION = "0.5.1"
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 = resource.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 = resource.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.3.0
4
+ version: 0.5.1
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-29 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: