Sha256: 607872355cc49324cffe5dd99acdd4f2970d62bb35ea1f5b914cb7a31a8701c7

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module Paysio
  class Form
    attr_accessor :params, :values, :errors, :html_options

    def initialize(params = {})
      @params = options[:params]
      @values = {}
      @errors = {}
      @html_options = {}
    end

    def add_params(params = {})
      self.params.merge!(params)
    end

    def render
      "#{render_html} #{render_javascript}".html_safe
    end

    def render_javascript
      html = <<-TEXT
        <script type="text/javascript">
          Paysio.setEndpoint('#{Paysio.api_url}');
          Paysio.setPublishableKey("#{Paysio.publishable_key}");
          Paysio.form.build(#{javascript_form_params});
        </script>
      TEXT
    end

    def render_html
      html = <<-TEXT
        <form id="#{@html_options[:id] || 'paysio'}" class="#{@html_options[:class]}"></form>
      TEXT
    end

    private
      def javascript_form_params
        params_html = "$('##{@html_options[:id]}'), #{@params.to_json}, #{@values.to_json}"
        params_html << ", #{@errors.to_json}" if @errors.present?
        params_html
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
paysio-1.0.9 lib/paysio/form.rb
paysio-1.0.8 lib/paysio/form.rb