Sha256: e6c9fbddf44cc31d0584ffb5d94443539a8af0c2984f1739b48fb2fe929009b1

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

require 'generators/cantango/basic'

module Cantango
  module Generators
    module PermitGenerator
      attr_accessor :permit_name, :permit_logic

      include Cantango::Generators::Basic

      def template_permit name, account = nil
        @permit_name = name
        set_logic name
        account.present? ? template_account_permit(name, account) : template_simple_permit(name)
      end

      def template_simple_permit name
        template permit_source, "app/permits/#{permit_target(name)}"
      end

      def template_account_permit name, account
        template "account_permit.erb" , "app/permits/#{account}/#{permit_target(name)}"
      end

      def load_permit_template name
        template = ERB.new File.open(template_filepath).read.gsub(/\n/, "\n\s\s")
        template.result(binding)
      end

      def template_filepath
        File.join source_path, permit_source
      end

      def source_path
        source_paths.first
      end

      def permit_source
        return "user_permit.erb" if is_user?
        return "account_permit.erb" if is_account?

        is_group? ? "role_group_permit.erb" : "role_permit.erb"
      end

      def permit_target name
        name = name.to_s.underscore
        return "#{name}_permit.rb" if is_user?
        return "#{name}_account_permit.rb" if is_account?

        is_group? ? "#{name}_role_group_permit.rb" : "#{name}_role_permit.rb"
      end

      def set_logic name
        meth = "#{name}_logic"
        @permit_logic = respond_to?(meth) ? send(meth) : base_logic
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 3 rubygems

Version Path
cantango-permits-0.1.1 lib/generators/cantango/permit_generator.rb
cantango-roles-0.1.0 lib/generators/cantango/permit_generator.rb
cantango-0.9.4.7 lib/generators/cantango/permit_generator.rb
cantango-0.9.4.6 lib/generators/cantango/permit_generator.rb
cantango-0.9.4.5 lib/generators/cantango/permit_generator.rb
cantango-0.9.4.3 lib/generators/cantango/permit_generator.rb
cantango-0.9.4.2 lib/generators/cantango/permit_generator.rb