Class: HtmlTo::HtmlToImageSettings

Inherits:
Object
  • Object
show all
Defined in:
lib/html_to.rb

Constant Summary collapse

OPTIONS =
%i[
  template width height image_name
].freeze
DEFAULT_OPTIONS =
{
  template: :circle,
  width: 1200,
  height: 630,
  image_name: :meta_image
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(klass, serializer, options, &block) ⇒ HtmlToImageSettings

Returns a new instance of HtmlToImageSettings.



19
20
21
22
23
24
25
26
# File 'lib/html_to.rb', line 19

def initialize(klass, serializer, options, &block)
  @klass = klass
  @options = options
  validate_options
  @serializer = serializer.to_s
  setup_options
  instance_exec(&block) if block_given?
end

Instance Method Details

#add_image(image_name, serializer, options = {}) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/html_to.rb', line 43

def add_image(image_name, serializer, options = {})
  @additional_images ||= {}
  options[:image_name] = image_name
  @klass.class_eval do
    has_one_attached image_name
  end
  @additional_images[image_name] = HtmlToImageSettings.new(@klass, serializer, options)
end

#additional_imagesObject



39
40
41
# File 'lib/html_to.rb', line 39

def additional_images
  @additional_images || {}
end

#find_template_path!Object

Raises:

  • (ArgumentError)


52
53
54
55
56
57
58
59
60
61
# File 'lib/html_to.rb', line 52

def find_template_path!
  user_template = Rails.root.join('app/views/html_to').join("#{template}.html.erb")
  return user_template.to_s if File.file? user_template

  gem_template = File.expand_path("views/html_to/#{template}.html.erb", __dir__)

  return gem_template.to_s if File.file? gem_template

  raise ArgumentError, "html_to error Template file not found #{user_template} #{gem_template} "
end

#setup_optionsObject



32
33
34
35
36
37
# File 'lib/html_to.rb', line 32

def setup_options
  OPTIONS.each do |k|
    instance_variable_set("@#{k}", @options[k] || DEFAULT_OPTIONS[k])
  end
  find_template_path!
end

#validate_optionsObject



28
29
30
# File 'lib/html_to.rb', line 28

def validate_options
  @options.each_key { |opt| raise ArgumentError, "html_to error #{opt} is unknown option" unless OPTIONS.include? opt }
end