Class: Article::Factory

Inherits:
Object
  • Object
show all
Defined in:
app/models/article/factory.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blog, user) ⇒ Factory

Returns a new instance of Factory.



6
7
8
9
# File 'app/models/article/factory.rb', line 6

def initialize(blog, user)
  @blog = blog
  @user = user
end

Instance Attribute Details

#blogObject (readonly)

Returns the value of attribute blog.



4
5
6
# File 'app/models/article/factory.rb', line 4

def blog
  @blog
end

#userObject (readonly)

Returns the value of attribute user.



4
5
6
# File 'app/models/article/factory.rb', line 4

def user
  @user
end

Instance Method Details

#defaultObject



11
12
13
14
15
16
17
18
# File 'app/models/article/factory.rb', line 11

def default
  blog.articles.build.tap do |art|
    art.allow_comments = blog.default_allow_comments
    art.allow_pings = blog.default_allow_pings
    art.text_filter_name = default_text_filter
    art.state = "draft"
  end
end

#default_text_filterObject



61
62
63
# File 'app/models/article/factory.rb', line 61

def default_text_filter
  user.text_filter_name || blog.text_filter
end

#extract_params(path, format) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/article/factory.rb', line 36

def extract_params(path, format)
  specs = format.split("/")
  specs.delete("")
  parts = path.split("/")
  parts.delete("")

  return if parts.length != specs.length

  specs.zip(parts).reduce({}) do |result, (spec, item)|
    if spec =~ /(.*)%(.*)%(.*)/
      before_format = Regexp.last_match[1]
      format_string = Regexp.last_match[2]
      after_format = Regexp.last_match[3]
      item =~ /^#{before_format}(.*)#{after_format}$/
      break unless Regexp.last_match

      value = Regexp.last_match[1]
      result[format_string.to_sym] = value
    elsif spec != item
      break
    end
    result
  end
end

#get_or_build_from(id) ⇒ Object



20
21
22
23
24
# File 'app/models/article/factory.rb', line 20

def get_or_build_from(id)
  return blog.articles.find(id) if id.present?

  default
end


26
27
28
29
# File 'app/models/article/factory.rb', line 26

def match_permalink_format(path, format)
  article_params = extract_params(path, format)
  requested_article(article_params) if article_params
end

#requested_article(params = {}) ⇒ Object



31
32
33
34
# File 'app/models/article/factory.rb', line 31

def requested_article(params = {})
  params[:title] ||= params[:article_id]
  Article.requested_article(params)
end