Sha256: 818a15ece39b456cf2646c278bd9284053a08d1f92076068fc9c1c6631eabfa1

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

module RailsVueHelpers
  module TagHelpers
    def link_to(name = nil, options = nil, html_options = nil, &block)
      options = html_options unless block_given?

      rails_tag_with_vue_data(super, options, :a)
    end

    def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)
      options = [content_or_options_with_block, options].find do |argument|
        argument.is_a?(Hash)
      end

      rails_tag_with_vue_data(super, options)
    end

    def check_box_tag(name, value = '1', checked = false, options = {})
      rails_tag_with_vue_data(super, options, :input)
    end

    private

    def rails_tag_with_vue_data(rails_tag, options, tag_name = nil)
      regex = rails_tag[/<#{tag_name}.+?>/][/(?:(?!>).)*/]

      rails_tag.gsub(regex, "\\0 #{vue_data(options)}").html_safe
    end

    def vue_data(options)
      options  ||= {}
      attributes = (options[:vue] || options['vue'] || {}).stringify_keys
      options    = {}

      (attributes['bind'] || {}).each do |key, value|
        options[":#{key}"] = value
      end

      (attributes['on'] || {}).each do |key, value|
        options["@#{key}"] = value
      end

      (attributes['directives'] || {}).each do |key, value|
        options["v-#{key}"] = value.to_s
      end

      (attributes['props'] || {}).each do |key, value|
        options[key] = value.to_s
      end

      if attributes['model']
        options['v-model'] = attributes['model']
      end

      remove_quotation_marks(options)
      tag_builder.tag_options(options)
    end

    def remove_quotation_marks(options)
      options.each do |key, value|
        return unless value
        next if value[0] != "\""
        next if value[-1] != "\""

        value[0] = ''
        value[-1] = ''
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rails-vue-helpers-0.1.2 lib/rails-vue-helpers/tag_helpers.rb
rails-vue-helpers-0.1.1 lib/rails-vue-helpers/tag_helpers.rb
rails-vue-helpers-0.1.0 lib/rails-vue-helpers/tag_helpers.rb