Sha256: b571aac5fc25b2f514bf7c54b7e7d4dd236a8a23f1a072aa0f77f33c4fc35f37

Contents?: true

Size: 861 Bytes

Versions: 2

Compression:

Stored size: 861 Bytes

Contents

class ViewComponent::ViewComponentModel
  attr_reader :component_name, :html_identifier, :html_classes
  def initialize id
    @component_name = self.class.name.match(/^(?<name>[^:]+)::/)[:name].underscore
    @html_identifier = "#{@component_name}_#{id}"
    @html_classes = ["view_component", "#{@component_name}_view_component"]
  end
  
  def responsibility
    1
  end

  def javascripts
    []
  end
  def stylesheets
    []
  end

  def html erb_params={}, ajax_params={}
    action_view = ActionView::Base.new "#{Rails.root}/app/view_components/#{@component_name}/views"
    erb_params.each{|k,v| action_view.assign(k=>v) }
    data = ajax_params.map{|k,v| "data-#{k}='#{v}'" }.join(' ')
    return <<HTML
<div id="#{@html_identifier}" class="#{@html_classes.join(' ')}" #{data}>
  #{action_view.render(template: self.erb_script)}
</div>
HTML
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_view_component-0.0.3 lib/generators/rails_view_component/templates/view_component/view_component_model.rb
rails_view_component-0.0.2 lib/generators/rails_view_component/templates/view_component/view_component_model.rb