Sha256: 24db46f13d4d3847101ba8b9a250e42578bed7c83d8c2ca8d7a19c82ff626da5

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

module RSpec::Rails
  module View
    include RSpec::Rails::BaseHelper
    
    # CREATE
    def create_view name, options = {}, &block
      file = view_file_name(name, options[:action], options[:type])
      dir = File.dirname(file)
      FileUtils.mkdir_p dir if !File.directory?(dir)
      content = options[:content]
      # set content to block
      content ||= yield if block

      # abort if no content given
      return if !content    
      
      # write file content of view
      File.open(file, 'w') do |f|  
        f.puts content 
      end
    end  

    # READ
    def read_view(name, options = {}, &block)
      file_name = view_file_name(name, options[:action], options[:type])
      debug "reading from: #{file_name}"
      content = File.new(file_name).try :read
      debug "read content: #{content}"
      yield content if block
      content
    end
    

    # UPDATE
    def insert_into_view(name, options = {}, &block)
      debug "insert_into_view: #{options}"
      file = view_file_name(name, options[:action], options[:type])
      debug "file insertion (view): #{file}"
      marker = options[:before] || options[:after]
      file_insertion(file, marker, options, &block) if marker
    end
    alias_method :update_view, :insert_into_view

    # DELETE
    def remove_view name, action=nil, type=nil
      file = view_file_name(name, action, type)
      FileUtils.rm_f(file) if File.exist?(file)
    end
    alias_method :delete_view, :remove_view

    def get_action action
      action || 'show'
    end

    def get_type type
      type || 'html.erb'
    end
    
    def view_file_name name, action=nil, type=nil
      File.join(view_dir, name.to_s, "#{get_action action}.#{get_type type}")
    end        

    aliases_for :view 
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
generator-spec-0.5.0 lib/generator_spec/rails_helpers/rails_view.rb