action_crud 0.1.4 → 0.1.5

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: 2bec677719c37b510e60fd122623b1b5ca4bb7f1
4
- data.tar.gz: ee0acbd7660e0b4ce61f3aba0e8df935d63468bd
3
+ metadata.gz: d00252806780a27d33fe1a5e0da095fc3800fc9a
4
+ data.tar.gz: 5f474851b6b228f456d0e52c7a8c3598e89b3b8a
5
5
  SHA512:
6
- metadata.gz: 5cd7315126fe152cf5575ab417fd73ea357622d403b43089e255a85fa289d5d45865c7c8a4f3cc1ec050c6c9e526312b5c6efa350621972b4d0c5991b11854c3
7
- data.tar.gz: c7362315dbaf55802ab75f05f75220672e434508b5d6a082b0738c5d1f2c62dbaa54eb9ea84212e0d10baf69062206fa970b15aafd404a15ae4597c24194a352
6
+ metadata.gz: 419b05fb125bdbf81933f2bbecd47a3e6e9960926b196ebfba20fabb7f3c7615d03dbef53bb77b85a778b2d110ff340ed2198a9fd371e42a2669cb607967be4f
7
+ data.tar.gz: d4c84b8008fc65dd9b15d757c33a465774d69738c387848605887bd4fd34437d52ff54df125c19df5ce0ac99dbac19ce16cd0829b2e52cc0d8b2d9a539a9243f
@@ -4,6 +4,7 @@ module ActionCrud
4
4
 
5
5
  included do
6
6
  # Class attributes
7
+ class_attribute :namespace, instance_predicate: false
7
8
  class_attribute :model_name, instance_predicate: false
8
9
  class_attribute :model_class, instance_predicate: false
9
10
  class_attribute :instance_name, instance_predicate: false
@@ -38,6 +39,12 @@ module ActionCrud
38
39
  self.permitted_params = permit
39
40
  end
40
41
 
42
+ # Set namespace to remove from model
43
+ def set_namespace(name=nil)
44
+ self.namespace = name
45
+ set_model_name
46
+ end
47
+
41
48
  # Set model name
42
49
  def set_model_name(name=nil)
43
50
  name = name || _guess_controller_model
@@ -66,8 +73,12 @@ module ActionCrud
66
73
  private
67
74
 
68
75
  def _guess_controller_model
69
- controller_name.classify.safe_constantize ||
70
- name.sub(/controller/i, '').classify.safe_constantize
76
+ if namespace.nil?
77
+ controller_name.classify.safe_constantize ||
78
+ name.gsub(/controller$/i, '').classify.safe_constantize
79
+ else
80
+ name.gsub(/^#{namespace.to_s}|controller$/i, '').classify.safe_constantize
81
+ end
71
82
  end
72
83
  end
73
84
 
@@ -39,6 +39,8 @@ module ActionCrud
39
39
  actions = args.concat(action).concat(options.keys)
40
40
  links = actions.uniq.map do |item|
41
41
  args = Hash(options[item]).reverse_merge(default)
42
+ args = args.merge(class: "#{default[:class]} #{args[:class]}".strip)
43
+
42
44
  ActionCrud::Helpers::Link.new(@context, record, item, args).render
43
45
  end
44
46
 
@@ -1,16 +1,16 @@
1
1
  module ActionCrud
2
2
  module Helpers
3
3
  class Route
4
- attr_accessor :record, :action, :options, :path, :url
4
+ attr_accessor :model, :record, :action, :options, :path, :url
5
5
 
6
6
  # Intialize url finder
7
7
  def initialize(context, record=nil, action=nil, *options)
8
8
  @context = context
9
9
  @record = record || @context.try(:current_record)
10
10
  @action = action
11
- @options = options
12
- @path = route_uri :path
13
- @url = route_uri :url
11
+ @options = Hash(options.first)
12
+ @path = route_uri true
13
+ @url = route_uri false
14
14
  end
15
15
 
16
16
  # To string
@@ -18,35 +18,51 @@ module ActionCrud
18
18
  instance_variable_get("@#{type}").to_s
19
19
  end
20
20
 
21
- # Is index action
22
- def index?
23
- action == :index
21
+ # Should include record
22
+ def record?
23
+ action.in? [:show, :edit, :delete, :destroy]
24
24
  end
25
25
 
26
- # Should prefix method
27
- def prefix?
28
- action.in? [:new, :edit]
26
+ # Get model name
27
+ def model(method='singular')
28
+ unless @record.nil?
29
+ @record.class.model_name.send(method).to_sym
30
+ end
29
31
  end
30
32
 
31
- # Should include record
32
- def record?
33
- action.in? [:show, :edit, :delete, :destroy]
33
+ # Get route namespace
34
+ def namespace
35
+ if @context.respond_to? :controller
36
+ @context.controller.try(:namespace)
37
+ else
38
+ @context.try(:namespace)
39
+ end
40
+ end
41
+
42
+ # Get namespaced record
43
+ def namespaced_record
44
+ if action == :edit
45
+ namespace.present? ? [:edit, namespace, record] : [:edit, record]
46
+ else
47
+ namespace.present? ? [namespace, record] : [record]
48
+ end
34
49
  end
35
50
 
36
- # Find route key
37
- def route_key
38
- singular = 'singular_' unless index?
39
- record.model_name.try :"#{singular}route_key"
51
+ # Get namespaced model
52
+ def namespaced_model
53
+ if action == :new
54
+ namespace.present? ? [:new, namespace, model] : [:new, model]
55
+ else
56
+ namespace.present? ? [namespace, model('plural')] : model('plural')
57
+ end
40
58
  end
41
59
 
42
60
  # Find route method
43
- def route_uri(type)
44
- args = [*options]
45
- args = [record, *options] if record?
46
- method = "#{route_key}_#{type}"
47
- method = "#{action}_#{method}" if prefix?
61
+ def route_uri(only_path=false)
62
+ args = record? ? namespaced_record : namespaced_model
63
+ args = [args, options.merge(only_path: only_path)].flatten.reject(&:blank?)
48
64
 
49
- @context.try :"#{method}", *args
65
+ @context.url_for(args)
50
66
  end
51
67
  end
52
68
  end
@@ -1,3 +1,3 @@
1
1
  module ActionCrud
2
- VERSION = '0.1.4'
2
+ VERSION = '0.1.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_crud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonian Guveli
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-17 00:00:00.000000000 Z
11
+ date: 2017-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack