activeadmin_addons 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 528fbd49fdbf8ac382f410b426eb2d6cd2caf1b0
4
- data.tar.gz: 4efd5f0aa574dde7fbbab34bdbd8f0eb78522e23
3
+ metadata.gz: 642e1d8cf2a3405355d4ff9c633121f219d69041
4
+ data.tar.gz: 136a74147270d9fab01136edc717d8f7a358d143
5
5
  SHA512:
6
- metadata.gz: 10870bf255ec311c4c800173d9e2d808aab2cf57a1bde264cc019ebcd7484f39b4d58f0963985efe81628bd36b2aaa487164f78b22f659f831b8a03fe9f6115a
7
- data.tar.gz: f2f0a3bc5c2e9426a22b1e75e4b36107159cb5a7293f5d32b5afc2d4636274aaba853e99a57d69b2065465e9873f1cf8ae70be7881c223c3176a79cabbb2d7dc
6
+ metadata.gz: b00492b0c016a1ff8191f227913ba2d1ed5ea3137614d1f673e37147456b420cdb85fb65386926961474e3f4b1876b3bbdcb6ac021bac5170a78d22b6f236dd0
7
+ data.tar.gz: ddd0f770ceeb567dfe2f942e23d7ec06160415bb61c743fd73f66d65456a6d239467e36e0099252d286dc247c49ae00294217a41338a5b4ac4292a66b47d370b
@@ -1,40 +1,41 @@
1
1
  module ActiveAdminAddons
2
- DEFAULT_BOOLEAN_TRUE = '✔'
3
- DEFAULT_BOOLEAN_FALSE = '✗'
2
+ class BoolBuilder < CustomBuilder
3
+ DEFAULT_BOOLEAN_TRUE = '&#x2714;'
4
+ DEFAULT_BOOLEAN_FALSE = '&#x2717;'
4
5
 
5
- module BoolValues
6
- class << self
7
- def bool_value(model, attribute)
8
- if model[attribute]
9
- i18n_lookup(model, attribute, 'true_value', DEFAULT_BOOLEAN_TRUE)
10
- else
11
- i18n_lookup(model, attribute, 'false_value', DEFAULT_BOOLEAN_FALSE)
12
- end
6
+ def render
7
+ if data
8
+ i18n_lookup('true_value', DEFAULT_BOOLEAN_TRUE)
9
+ else
10
+ i18n_lookup('false_value', DEFAULT_BOOLEAN_FALSE)
13
11
  end
12
+ end
14
13
 
15
- private
14
+ private
16
15
 
17
- def i18n_lookup(model, attribute, key, last_default)
18
- model_name = model.class.model_name.i18n_key
16
+ def i18n_lookup(key, last_default)
17
+ model_name = model.class.model_name.i18n_key
19
18
 
20
- model_i18n = "activeadmin.addons.boolean.models.#{model_name}.#{key}"
21
- default_i18n = "activeadmin.addons.boolean.default.#{key}"
19
+ model_i18n = "activeadmin.addons.boolean.models.#{model_name}.#{key}"
20
+ default_i18n = "activeadmin.addons.boolean.default.#{key}"
22
21
 
23
- I18n.t(model_i18n, default: I18n.t(default_i18n, default: last_default)).html_safe
24
- end
22
+ I18n.t(model_i18n, default: I18n.t(default_i18n, default: last_default)).html_safe
25
23
  end
26
24
  end
27
25
 
28
- module ::ActiveAdmin
29
- module Views
30
- class TableFor
31
- def bool_column(attribute)
32
- column(attribute) { |model| BoolValues.bool_value(model, attribute) }
26
+ module BoolValues
27
+
28
+ module ::ActiveAdmin
29
+ module Views
30
+ class TableFor
31
+ def bool_column(*args, &block)
32
+ column(*args) { |model| BoolBuilder.render(self, model, *args, &block) }
33
+ end
33
34
  end
34
- end
35
- class AttributesTable
36
- def bool_row(attribute)
37
- row(attribute) { |model| BoolValues.bool_value(model, attribute) }
35
+ class AttributesTable
36
+ def bool_row(*args, &block)
37
+ row(*args) { |model| BoolBuilder.render(self, model, *args, &block) }
38
+ end
38
39
  end
39
40
  end
40
41
  end
@@ -1,25 +1,22 @@
1
1
  module ActiveAdminAddons
2
- module EnumTag
3
- class << self
4
- def tag(context, model, attribute, options)
5
- state = model.send(attribute)
6
- raise 'you need to install enumerize gem first' unless defined? Enumerize::Value
7
- raise 'you need to pass an enumerize attribute' unless state.is_a?('Enumerize::Value'.constantize)
8
- context.status_tag(state.text, state)
9
- end
2
+ class EnumBuilder < CustomBuilder
3
+ def render
4
+ raise 'you need to install enumerize gem first' unless defined? Enumerize::Value
5
+ raise 'you need to pass an enumerize attribute' unless data.is_a?('Enumerize::Value'.constantize)
6
+ context.status_tag(data.text, data)
10
7
  end
11
8
  end
12
9
 
13
10
  module ::ActiveAdmin
14
11
  module Views
15
12
  class TableFor
16
- def tag_column(attribute, options = {})
17
- column(attribute) { |model| EnumTag.tag(self, model, attribute, options) }
13
+ def tag_column(*args, &block)
14
+ column(*args) { |model| EnumBuilder.render(self, model, *args, &block) }
18
15
  end
19
16
  end
20
17
  class AttributesTable
21
- def tag_row(attribute, options = {})
22
- row(attribute) { |model| EnumTag.tag(self, model, attribute, options) }
18
+ def tag_row(*args, &block)
19
+ row(*args) { |model| EnumBuilder.render(self, model, *args, &block) }
23
20
  end
24
21
  end
25
22
  end
@@ -1,70 +1,61 @@
1
- module ActiveAdminAddons
2
- module ListHelper
3
- class << self
4
- def localized_value(key, model, attribute)
5
- I18n.t("addons_list.#{model.class.name.underscore}.#{attribute}.#{key}")
6
- end
1
+ class ListBuilder < ActiveAdminAddons::CustomBuilder
7
2
 
8
- def array_list(ctx, model, attribute, options)
9
- items = model.send(attribute)
10
- ctx.content_tag(options[:list_type]) do
11
- items.each do |value|
12
- value = localized_value(value, model, attribute) if !!options[:localize]
13
- ctx.concat(ctx.content_tag(:li, value))
14
- end
15
- end
16
- end
3
+ def render
4
+ options[:localize] = options.fetch(:localize, false)
5
+ options[:list_type] = options.fetch(:list_type, :ul)
17
6
 
18
- def hash_list(ctx, model, attribute, options)
19
- items = model.send(attribute)
20
- ctx.content_tag(options[:list_type]) do
21
- items.keys.each do |key|
22
- label = !!options[:localize] ? localized_value(key, model, attribute) : key
23
- value = items[key]
24
- ctx.concat(ctx.content_tag(:li) do
25
- if value.blank?
26
- ctx.content_tag(:span, label)
27
- else
28
- ctx.content_tag(:span) do
29
- ctx.concat("#{label}:&nbsp".html_safe)
30
- ctx.concat(ctx.content_tag(:span) do
31
- ctx.content_tag(:i, value)
32
- end)
33
- end
34
- end
35
- end)
36
- end
37
- end
38
- end
7
+ raise 'invalid list type (ul, ol)' unless [:ul, :ol].include?(options[:list_type])
8
+ raise "list must be Array or Hash" if !data.is_a?(Hash) && !data.is_a?(Array)
39
9
 
40
- def list(ctx, model, attribute, options)
41
- options[:localize] = options.fetch(:localize, false)
42
- options[:list_type] = options.fetch(:list_type, :ul)
43
- raise 'invalid list type (ul, ol)' unless [:ul, :ol].include?(options[:list_type])
10
+ data.is_a?(Array) ? render_array : render_hash
11
+ end
44
12
 
45
- items = model.send(attribute)
13
+ def localized_value(key, model, attribute)
14
+ I18n.t("addons_list.#{model.class.name.underscore}.#{attribute}.#{key}")
15
+ end
46
16
 
47
- if !items.is_a?(Hash) && !items.is_a?(Array)
48
- rails "list must be Array or Hash"
49
- end
17
+ def render_array
18
+ context.content_tag(options[:list_type]) do
19
+ data.each do |value|
20
+ value = localized_value(value, model, attribute) if !!options[:localize]
21
+ context.concat(context.content_tag(:li, value))
22
+ end
23
+ end
24
+ end
50
25
 
51
- return array_list(ctx, model, attribute, options) if items.is_a?(Array)
52
- hash_list(ctx, model, attribute, options)
26
+ def render_hash
27
+ context.content_tag(options[:list_type]) do
28
+ data.keys.each do |key|
29
+ label = !!options[:localize] ? localized_value(key, model, attribute) : key
30
+ value = data[key]
31
+ context.concat(context.content_tag(:li) do
32
+ if value.blank?
33
+ context.content_tag(:span, label)
34
+ else
35
+ context.content_tag(:span) do
36
+ context.concat("#{label}:&nbsp".html_safe)
37
+ context.concat(context.content_tag(:span) do
38
+ context.content_tag(:i, value)
39
+ end)
40
+ end
41
+ end
42
+ end)
53
43
  end
54
44
  end
55
45
  end
56
46
 
57
- module ::ActiveAdmin
58
- module Views
59
- class TableFor
60
- def list_column(attribute, options = {})
61
- column(attribute) { |model| ListHelper.list(self, model, attribute, options) }
62
- end
47
+ end
48
+
49
+ module ::ActiveAdmin
50
+ module Views
51
+ class TableFor
52
+ def list_column(*args, &block)
53
+ column(*args) { |model| ::ListBuilder.new(self, model, *args, &block).render }
63
54
  end
64
- class AttributesTable
65
- def list_row(attribute, options = {})
66
- row(attribute) { |model| ListHelper.list(self, model, attribute, options) }
67
- end
55
+ end
56
+ class AttributesTable
57
+ def list_row(*args, &block)
58
+ row(*args) { |model| ::ListBuilder.new(self, model, *args, &block).render }
68
59
  end
69
60
  end
70
61
  end
@@ -1,37 +1,34 @@
1
1
  module ActiveAdminAddons
2
- module NumberHelper
3
- class << self
4
- NUMBER_TYPES = {
5
- currency: :number_to_currency,
6
- human: :number_to_human,
7
- human_size: :number_to_human_size,
8
- percentage: :number_to_percentage,
9
- phone: :number_to_phone,
10
- delimiter: :number_with_delimiter,
11
- precision: :number_with_precision
12
- }
2
+ class NumberBuilder < CustomBuilder
3
+ NUMBER_TYPES = {
4
+ currency: :number_to_currency,
5
+ human: :number_to_human,
6
+ human_size: :number_to_human_size,
7
+ percentage: :number_to_percentage,
8
+ phone: :number_to_phone,
9
+ delimiter: :number_with_delimiter,
10
+ precision: :number_with_precision
11
+ }
13
12
 
14
- def label(context, model, attribute, options)
15
- options[:as] = options.fetch(:as, :delimiter)
16
- if !NUMBER_TYPES.keys.include?(options[:as])
17
- raise "Invalid number type. Options are: #{NUMBER_TYPES.keys.to_s}"
18
- end
19
- number = model.send(attribute)
20
- context.send(NUMBER_TYPES[options[:as]], number, options)
13
+ def render
14
+ options[:as] = options.fetch(:as, :delimiter)
15
+ if !NUMBER_TYPES.keys.include?(options[:as])
16
+ raise "Invalid number type. Options are: #{NUMBER_TYPES.keys.to_s}"
21
17
  end
18
+ context.send(NUMBER_TYPES[options[:as]], data, options)
22
19
  end
23
20
  end
24
21
 
25
22
  module ::ActiveAdmin
26
23
  module Views
27
24
  class TableFor
28
- def number_column(attribute, options = {})
29
- column(attribute) { |model| NumberHelper.label(self, model, attribute, options) }
25
+ def number_column(*args, &block)
26
+ column(*args) { |model| NumberBuilder.render(self, model, *args, &block) }
30
27
  end
31
28
  end
32
29
  class AttributesTable
33
- def number_row(attribute, options = {})
34
- row(attribute) { |model| NumberHelper.label(self, model, attribute, options) }
30
+ def number_row(*args, &block)
31
+ row(*args) { |model| NumberBuilder.render(self, model, *args, &block) }
35
32
  end
36
33
  end
37
34
  end
@@ -1,56 +1,53 @@
1
1
  module ActiveAdminAddons
2
- module PaperclipAttachment
3
- class << self
4
- KNOWN_EXTENSIONS = %w{
2
+ class PaperclipAttachmentBuilder < CustomBuilder
3
+ KNOWN_EXTENSIONS = %w{
5
4
  3gp 7z ace ai aif aiff amr asf asx bat bin bmp bup cab cbr cda cdl cdr chm
6
5
  dat divx dll dmg doc docx dss dvf dwg eml eps exe fla flv gif gz hqx htm html
7
6
  ifo indd iso jar jpeg jpg lnk log m4a m4b m4p m4v mcd mdb mid mov mp2 mp4
8
7
  mpeg mpg msi mswmm ogg pdf png pps ppt pptx ps psd pst ptb pub qbb qbw qxd ram
9
8
  rar rm rmvb rtf sea ses sit sitx ss swf tgz thm tif tmp torrent ttf txt
10
9
  vcd vob wav wma wmv wps xls xlsx xpi zip
11
- }
10
+ }
12
11
 
13
- def icon_for_filename filename
14
- for_ext File.extname(filename)
15
- end
16
-
17
- def for_ext file_extension
18
- ext = file_extension.start_with?('.') ? file_extension[1..-1] : file_extension
19
- ext.downcase!
20
- ext = "unknown" unless KNOWN_EXTENSIONS.include?(ext)
21
- "fileicons/file_extension_#{ext}.png"
22
- end
12
+ def icon_for_filename filename
13
+ for_ext File.extname(filename)
14
+ end
23
15
 
24
- def link(context, model, attribute, options)
25
- options[:truncate] = options.fetch(:truncate, true)
26
- doc = model.send(attribute)
27
- return nil if doc.nil?
28
- raise 'you need to pass a paperclip attribute' unless doc.respond_to?(:url)
16
+ def for_ext file_extension
17
+ ext = file_extension.start_with?('.') ? file_extension[1..-1] : file_extension
18
+ ext.downcase!
19
+ ext = "unknown" unless KNOWN_EXTENSIONS.include?(ext)
20
+ "fileicons/file_extension_#{ext}.png"
21
+ end
29
22
 
30
- icon = icon_for_filename(doc.original_filename)
31
- icon_img = context.image_tag(icon, width: "20", height: "20", style: "margin-right: 5px; vertical-align: middle;")
32
- file_name = (options[:truncate] == true) ? context.truncate(doc.original_filename) : doc.original_filename
33
- label_text = options.fetch(:label, file_name)
34
- label = context.content_tag(:span) do
35
- context.concat(icon_img)
36
- context.safe_concat(label_text)
37
- end
23
+ def render
24
+ options[:truncate] = options.fetch(:truncate, true)
25
+ return nil if data.nil?
26
+ raise 'you need to pass a paperclip attribute' unless data.respond_to?(:url)
38
27
 
39
- context.link_to(label, doc.url, { target: "_blank" }) if doc.exists?
28
+ icon = icon_for_filename(data.original_filename)
29
+ icon_img = context.image_tag(icon, width: "20", height: "20", style: "margin-right: 5px; vertical-align: middle;")
30
+ file_name = (options[:truncate] == true) ? context.truncate(data.original_filename) : data.original_filename
31
+ label_text = options.fetch(:label, file_name)
32
+ label = context.content_tag(:span) do
33
+ context.concat(icon_img)
34
+ context.safe_concat(label_text)
40
35
  end
36
+
37
+ context.link_to(label, data.url, { target: "_blank" }) if data.exists?
41
38
  end
42
39
  end
43
40
 
44
41
  module ::ActiveAdmin
45
42
  module Views
46
43
  class TableFor
47
- def attachment_column(attribute, options = {})
48
- column(attribute) { |model| PaperclipAttachment.link(self, model, attribute, options) }
44
+ def attachment_column(*args, &block)
45
+ column(*args) { |model| PaperclipAttachmentBuilder.render(self, model, *args, &block) }
49
46
  end
50
47
  end
51
48
  class AttributesTable
52
- def attachment_row(attribute, options = {})
53
- row(attribute) { |model| PaperclipAttachment.link(self, model, attribute, options) }
49
+ def attachment_row(*args, &block)
50
+ row(*args) { |model| PaperclipAttachmentBuilder.render(self, model, *args, &block) }
54
51
  end
55
52
  end
56
53
  end
@@ -1,26 +1,23 @@
1
1
  module ActiveAdminAddons
2
- module PaperclipImage
3
- class << self
4
- def image(context, model, attribute, options)
5
- img = model.send(attribute)
6
- return nil if img.nil?
7
- raise 'you need to pass a paperclip image attribute' unless img.respond_to?(:url)
8
- style = options.fetch(:style, :original)
9
- context.image_tag(img.url(style)) if img.exists?
10
- end
2
+ class PaperclipImageBuilder < CustomBuilder
3
+ def render
4
+ return nil if data.nil?
5
+ raise 'you need to pass a paperclip image attribute' unless data.respond_to?(:url)
6
+ style = options.fetch(:style, :original)
7
+ context.image_tag(data.url(style)) if data.exists?
11
8
  end
12
9
  end
13
10
 
14
11
  module ::ActiveAdmin
15
12
  module Views
16
13
  class TableFor
17
- def image_column(attribute, options = {})
18
- column(attribute) { |model| PaperclipImage.image(self, model, attribute, options) }
14
+ def image_column(*args, &block)
15
+ column(*args) { |model| PaperclipImageBuilder.render(self, model, *args, &block) }
19
16
  end
20
17
  end
21
18
  class AttributesTable
22
- def image_row(attribute, options = {})
23
- row(attribute) { |model| PaperclipImage.image(self, model, attribute, options) }
19
+ def image_row(*args, &block)
20
+ row(*args) { |model| PaperclipImageBuilder.render(self, model, *args, &block) }
24
21
  end
25
22
  end
26
23
  end
@@ -3,6 +3,7 @@ module ActiveAdminAddons
3
3
  class Engine < ::Rails::Engine
4
4
  require 'select2-rails'
5
5
  initializer "set boolean values addon" do |app|
6
+ require_relative './support/custom_builder'
6
7
  require_relative './addons/bool_values'
7
8
  require_relative './addons/paperclip_image'
8
9
  require_relative './addons/paperclip_attachment'
@@ -0,0 +1,34 @@
1
+ module ActiveAdminAddons
2
+ class CustomBuilder
3
+ attr_accessor :context, :model, :args, :block
4
+
5
+ def initialize(context, model, *args, &block)
6
+ @context = context
7
+ @model = model
8
+ @args = *args
9
+ @block = block
10
+ end
11
+
12
+ def render
13
+ end
14
+
15
+ def self.render(context, model, *args, &block)
16
+ new(context, model, *args, &block).render
17
+ end
18
+
19
+ protected
20
+
21
+ def data
22
+ @data ||= block ? block.call(model) : model.send(attribute)
23
+ end
24
+
25
+ def options
26
+ @options ||= args.last.is_a?(Hash) ? args.last : {}
27
+ end
28
+
29
+ def attribute
30
+ @attribute ||= args[1] || args[0]
31
+ end
32
+
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveadminAddons
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeadmin_addons
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Platanus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-27 00:00:00.000000000 Z
11
+ date: 2015-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -216,6 +216,7 @@ files:
216
216
  - lib/activeadmin_addons/addons/paperclip_attachment.rb
217
217
  - lib/activeadmin_addons/addons/paperclip_image.rb
218
218
  - lib/activeadmin_addons/engine.rb
219
+ - lib/activeadmin_addons/support/custom_builder.rb
219
220
  - lib/activeadmin_addons/support/enumerize_formtastic_support.rb
220
221
  - lib/activeadmin_addons/support/set_datepicker.rb
221
222
  - lib/activeadmin_addons/version.rb