Module: Smooth::Resource::Templating

Included in:
Smooth::Resource
Defined in:
lib/smooth/resource/templating.rb

Constant Summary collapse

FakerGroups =
%w(
  Address Lorem Color Company Food HipterIpsum Internet Job Name Movie PhoneNumber Product Unit Vehicle Venue Skill
)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fakersObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/smooth/resource/templating.rb', line 8

def self.fakers
  FakerGroups.flat_map do |group|
    prefix = group.to_s.underscore.downcase
    space = Faker.const_get(group.to_sym) rescue nil

    if space && space.class == Module
      (space.methods - Object.methods - [:k, :underscore]).map { |m| "#{prefix}.#{m}" }
    end
  end.compact.uniq
end

Instance Method Details

#build_from_template(name = nil, *args, &block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/smooth/resource/templating.rb', line 27

def build_from_template(name = nil, *args, &block)
  if name.is_a?(Hash)
    args.unshift(name)
    name = @template_name
  end

  FactoryGirl.build(name || @template_name, *args, &block)
end

#create_from_template(name = nil, *args, &block) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/smooth/resource/templating.rb', line 19

def create_from_template(name = nil, *args, &block)
  if name.is_a?(Hash)
    args.unshift(name)
    name = @template_name
  end
  FactoryGirl.create(name || @template_name, *args, &block)
end

#template(name = nil, *args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/smooth/resource/templating.rb', line 36

def template(name = nil, *args, &block)
  options = args.extract_options!

  if name.nil?
    name = model_class.table_name.singularize.to_sym
    @template_name ||= name
  end

  options[:class] ||= model_class

  FactoryGirl.define do
    factory(name.to_sym, options, &block)
  end
end

#template_registered?(name = nil) ⇒ Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/smooth/resource/templating.rb', line 51

def template_registered?(name = nil)
  name ||= model_class.table_name.singularize.to_sym
  !!(FactoryGirl.factory_by_name(name) rescue nil)
end

#templates(&block) ⇒ Object

Just allows us to wrap template definitions



57
58
59
# File 'lib/smooth/resource/templating.rb', line 57

def templates(&block)
  instance_eval(&block)
end