Sha256: 20ff3592591a9e9efabe19e602505f6de673bdbeb966c3a830442d7dba840914

Contents?: true

Size: 1.04 KB

Versions: 12

Compression:

Stored size: 1.04 KB

Contents

module Spina
  module Embeddable
    extend ActiveSupport::Concern
    
    included do
      class_attribute :embed_attributes, default: []
      class_attribute :icon, default: nil
    end
    
    class_methods do
      def from_json(json)
        begin
          attributes = JSON.parse(json)
          attributes.transform_keys!(&:to_sym)
          new(attributes)
        rescue
          Rails.logger.error "[#{self.class.name}] Couldn't parse JSON"
          nil
        end
      end
      
      def attributes(*names)
        attr_accessor(*names.map(&:to_sym))
        self.embed_attributes += names.map(&:to_sym)
      end
      
      # Give it an icon
      def heroicon(name)
        self.icon = name
      end
    end
    
    def initialize(attributes = {})
      attributes.slice(*self.class.embed_attributes).each do |key, value|
        instance_variable_set("@#{key}", value)
      end
    end
    
    def to_fields_path
      "#{to_partial_path}_fields"
    end
    
    def to_trix_partial_path
      to_partial_path
    end
    
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
spina-2.12.0 lib/spina/embeddable.rb
spina-2.11.0 lib/spina/embeddable.rb
spina-2.10.0 lib/spina/embeddable.rb
spina-2.9.1 lib/spina/embeddable.rb
spina-2.9.0 lib/spina/embeddable.rb
spina-2.8.1 lib/spina/embeddable.rb
spina-2.8.0 lib/spina/embeddable.rb
spina-2.7.0 lib/spina/embeddable.rb
spina-2.6.2 lib/spina/embeddable.rb
spina-2.6.1 lib/spina/embeddable.rb
spina-2.6.0 lib/spina/embeddable.rb
spina-2.5.0 lib/spina/embeddable.rb